php操作MySQL資料庫的方法

來源:互聯網
上載者:User

標籤:

使用mysql_connect()函數串連MySQL伺服器

要操作MySQL資料庫,首先必須與MySQL伺服器建立串連。串連MySQL伺服器的語句如下:

mysql_connect(‘hostname‘,‘username‘,‘password‘);

該函數的傳回值表示這個資料庫連接。如果串連成功,則函數返回一個資源,為以後執行SQL指令作準備。

執行個體代碼如下:

<?php $link = mysql_connect("localhost", "root", "root") or die("不能串連到資料庫伺服器!可能是

資料庫伺服器沒有啟動,或者使用者名稱密碼有誤!".mysql_error());   //串連Mysql伺服器

   if($link){

   echo "資料連線成功!";

}

?>

運行結果:

 

使用mysql_select_db()函數選擇資料庫檔案

在串連到MySQL資料庫伺服器之後,接下來使用mysql_select_db()函數選擇資料庫。

文法格式如下:

mysql_select_db(string 資料庫名[,resource link_identifier])

mysql_qurey("use資料庫名"[.resource link_identifier]);

執行個體代碼如下:

<?php
$link = mysql_connect("localhost", "root", "root") or die("不能串連到資料庫伺服器!可能是資料庫伺服器沒有啟動,或者使用者名稱密碼有誤!".mysql_error());   //串連Mysql伺服器
$db_selected=mysql_select_db("db_database18",$link);
//$db_selected=mysql_query("use db_database18",$link);
if($db_selected){
echo "資料庫選擇成功!";
}
?>

運行結果:

 

php操作MySQL資料庫的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.