程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> MySQL數據庫函數詳解(2)

MySQL數據庫函數詳解(2)

編輯:關於MYSQL數據庫

(6) int MySQL_db_query(string db_name, string query [, int link_id]);

  MySQL_db_query()除了提取一個額外的數據庫名稱參數,並在執行查詢之前使它成為缺省的數據庫為,與MySQL_query()類似。

<?PHP

$link=MySQL_pconnect("localhost","sunsoft","suixiang")
or dIE("Could not connect");

print("Connected successfully");

$query="SELECT * FROM president";

$result=MySQL_db_query("samp_db",$query) or dIE("Query failed");


?>

(7) int MySQL_drop_db(string db_name, int [link_id]);

   告訴由link_id標識的MySQL服務器用給定的名稱來刪除數據庫。如果數據庫刪除成功,則返回真;如果出現錯誤,則返回假。必須有對數據庫進行刪除的DROP權限。

  要小心這個函數;如果刪除數據庫,它就不存在了,且不能恢復。

  使用mysql_query()較使用MySQL_drop_db()發布DROP DATABASE 語句更為適合。

<?PHP

$link=MySQL_pconnect("localhost","sunsoft","suixiang")
or dIE("Could not connect");

if(MySQL_drop_db("my_db"))

print("Database dropped successfullyn");

else

printf("Error dropping database:%sn",MySQL_error());

?>

(8) int MySQL_errno(int [link_id]);

  對於給定的連接,返回含有最近返回狀態的與MySQL相關的函數的錯誤號。零值意味著未出現錯誤。

使用范例

<?PHP

$link=MySQL_pconnect("localhost","sunsoft","suixiang")
or dIE("Could not connect");

print("Connected successfully");

$query="SELECT * FROM president";

$result=mysql_query($query) or dIE("Query failed,error code=".MySQL_errno());

?>

(9) string MySQL_error(int [link_id]);

   對於給定的連接,返回含有最近返回狀態的與MySQL相關的函數的錯誤消息字符串。空值意味著未出現錯誤。

<?PHP

$link=MySQL_pconnect("localhost","sunsoft","suixiang")
or dIE("Could not connect");

print("Connected successfully");

$query="SELECT * FROM president";

$result=mysql_query($query) or dIE("query failed,error message=".MySQL_error());

?>

(10)array MySQL_fetch_array(int result, int [result_typ]);

   本函式用來將查詢結果 result 拆到陣列變數中。若 result 沒有資料,則傳回 false 值。而本函式可以說是 mysql_fetch_row() 的加強函式,除可以將傳回列及數字索引放入陣列之外,還可以將文字索引放入陣列中。若是好幾個傳回欄位都是相同的文字名稱,則最後一個置入的欄位有效,解決方法是使用數字索引或者為這些同名的欄位 (column) 取別名 (alias)。值得注意的是使用本函式的處理速度其實不會比mysql_fetch_row() 函式慢,要用哪個函式還是看使用的需求決定。參數 result_typ 是一個常數值,有以下幾種常數 MYSQL_ASSOC、MYSQL_NUM 與 MySQL_BOTH。

使用范例

<?PHP

$link=MySQL_pconnect("localhost","sunsoft","suixiang")
or dIE("Could not connect");

MySQL_select_db("stamp_db") or dIE("Could not select database");


$query="SELECT last_name,first_name FROM president";

$result=MySQL_query($query) or dIE("Query failed");

while($row=MySQL_fetch_array($result))

{

printf("%s %s<BR>

",$row[0],$row[1]);

printf("%s %s<BR>

",$row["last_name"],$row["first_name"]);

}

MySQL_free_result($result);

?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved