« 66. PHP 的 Complex Expressions | (回到Blog入口) | 68. PHP 的影像繪圖函數群整理 »

67. PHP 的 MySQL 函數群整理

jollen 發表於 October 27, 2006 7:17 PM

mysql_close() 函數

bool mysql_close(resource [link_identifier]);

關閉 MySQL 連接。成功傳回 true,否則傳回 false。

範例:

$link = mysql_cconnect("jollen.idv.tw", "jollne", "akd83k"); mysql_close($link);

如果沒有傳入 link 變數,則以最後的 link 為主,也就是上例也可以寫成:

$link = mysql_connect("jollen.idv.tw:6677", "jollen", "rcakd83k"); mysql_close();

利用 mysql_connect() 開啟的 link 必須以 mysql_close() 關閉。而利用 mysql_pconnect() 開啟的 link 稱為 persistent link,persistent link 在 PHP 程式執行完畢後該連線仍會存在,利用 mysql_close() 也無法關閉 persistent link。

相關函數:mysql_connect(), mysql_pconnect()

mysql_connect() 函數

resource mysql_connect(string [hostname] [:port] , string [username] , string [password], bool [new_link], int client_flags);

開啟與 MySQL 的連接 (link),例如:

$link = mysql_connect("localhost:7000", "guest", "guest123");

連接成功時則傳回一個正整數的 link 變數,否則傳回 false。

如果沒有傳入參數的話,則使用預設參數。當 PHP 執行完時,會自動關閉這個 link,或者使用 mysql_close() 來關閉。

PHP 4.2.0 與之後的版本才能指定 new_link 參數,new_link 參數為 bool 型態,可指定 TRUE 或 FALSE。傳入 TRUE 的話可限定 mysql_connect() 一定要開啟新的連線。

PHP 4.3.0 與之後的版本增加 client_flags 參數,client_flags 可指定 3 個常數值如下:

1. MYSQL_CLIENT_COMPRESS
2. MYSQL_CLIENT_IGNORE_SPACE
3. MYSQL_CLIENT_INTERACTIVE

相關函數:mysql_pconnect(), and mysql_close().

mysql_create_db() 函數

bool mysql_create_db(string database name, resource [link_identifier]);

建立新的資料庫,成功傳回 true,否則傳回 false。例如:

$link = mysql_pconnect(); mysql_create_db("test", $link);

相關函數:mysql_drop_db(), mysql_createdb()

mysql_data_seek() 函數

bool mysql_data_seek(resource result_identifier, int row_number);

移動資料的內部指標。成功傳回 true,否則傳回 false。row_number 是資料的編號,由 0 開始。result_identifier 則是利用 mysql_query() 所傳回的結果,也就是指向資料的指標。

mysql_data_seek() 可配合 mysql_fetch_row() 來取得資料。注意 mysql_fetch_row() 傳回資料後會自動將資料內部指標往下指到下一個符合查詢條件的資料。

範例:

<?php $link = mysql_pconnect("localhost", "root", ""); mysql_select_db("guestbook", $link); $result = mysql_query("SELECT * FROM messages", $link); // table 必須至少有一筆資料 mysql_data_seek($result, 0); // 移到開頭 $fields = mysql_fetch_row($result); ?>

mysql_free_result() 函數

bool mysql_free_result(resource result);

釋放由 mysql_query() 傳回的 result 所佔的記憶體空間。事實上,PHP 會自動幫我們釋放 result 變數所佔的記憶體空間。mysql_free_result() 用到的機會很少,除非是特殊狀況,例如傳回的 result 可能佔掉太多記憶體空間。

mysql_list_tables() 函數

resource mysql_list_tables(string database, resource [link_identifier]);

列出指定的 database 裡有哪些 table。

mysql_num_fields() 函數

int mysql_num_fields(resource result);

傳回 result 的欄位 (field) 數量。我們可利用 mysql_num_rows() 取得由 mysql_query() 傳回的 result 欄位數量。

相關函數:mysql_db_query(), mysql_query(), mysql_fetch_field(), mysql_num_rows()

mysql_num_rows() 函數

int mysql_num_rows(resource result);

傳回 result 的列數。我們可利用 mysql_num_rows() 取得由 mysql_query() 傳回的 result 個數。

相關函數:mysql_affected_rows(), mysql_db_query(), mysql_query(), mysql_fetch_row()

mysql_pconnect() 函數

resource mysql_pconnect(string [hostname] [:port] , string [username] , string [password], int [client_flags]);

建立連 MySQL 的 persistent connection。成功傳回一個正整數的 link 值,否則傳回 false。

mysql_pconnect() 與 mysql_connect() 同為建立 MySQL 連線的函數,倒底兩者有何不同呢?一般而言,mysql_pconnect() 比 mysql_connect() 好用,兩者主要的差別為:

1. mysql_pconnect() 會檢查是否有重覆的 persistent link。即是否有相同 host、相同 username 與相同 password 的 connection,如果有則不會再重覆開啟。

2. 當 PHP 程式執行完畢時,利用 mysql_pconnect() 開啟的 connection 不會自動關閉。因為這個特性,這個 connection 可以被以後的程式使用。

利用 mysql_pconnect() 開啟的 connection 因為有上面介紹的這兩種特性,因此稱利用 mysql_pconnect() 開啟的 connection 為 persistent connection,即持續性 (persistent) 的連線。

只有將 PHP 以 module 方式安裝才能使用 persistent connection。CGI 版本並不支援。

PHP 4.3.0 與之後的版本增加 client_flags 參數,client_flags 可指定 3 個常數值如下:

1. MYSQL_CLIENT_COMPRESS
2. MYSQL_CLIENT_IGNORE_SPACE
3. MYSQL_CLIENT_INTERACTIVE

mysql_query() 函數

resource mysql_query(string query, resource [link_identifier] );

傳入一個 SQL 敘述給 MySQL。mysql_query() 可將 SELECT、UPDATE、INSERT 與 DELETE 的 SQL 敘述傳給 MySQL 做處理。如果是傳入 SELECT 敘述,則傳回一個指向傳回結果的指標。要注意傳回的只是一個指標,必須再利用 mysql_fetch_row() 才能取得資料內容。

利用 SELECT 取得的資料 (result) 不用時可以利用 mysql_free_result() 釋回空間。

相關函數:mysql_num_rows(), mysql_affected_rows(), mysql_unbuffered_query(), mysql_fetch_array(), mysql_fetch_assoc(), mysql_fetch_row(), mysql_db_query(), mysql_select_db(), mysql_connect()

mysql_select_db() 函數

bool mysql_select_db(string database_name, resource [link_identifier]);

選擇預設資料庫。成功傳回 true,否則傳回 false。例如:

$link = mysql_pconnect(); mysql_select_db("star", $link);

如果沒有指定 link,則 mysql_select_db() 會使用最後一個開啟的 link。選擇預設資料庫後,利用 mysql_query() 就可以使用該資料庫。

相關函數:mysql_connect(), mysql_pconnect(), and mysql_query()

mysql_affected_rows() 函數

int mysql_affected_rows(resource [link_identifier]);

傳回上一個動作真正影響的資料錄 (row) 列數。mysql_affected_rows() 接受一個連線代號,並傳回該連線最後的 INSERT、UPDATE 或 DELETE 影響 (改變) 的資料錄列數。我們可以利用這個函數來檢查最後實際 INSERT 的資料筆數,以確保所有的資料都有被 INSERT。

有一點要特別注意的是,如果最後是利用 DELETE 來刪除所有的資料錄 (沒有加上 WHERE 子句),則 mysql_affected_rows() 會傳回 0。這個函數並不支援傳回受 SELECT 影響的資料列數,如果想取得受 SELECT 影響的資料列數,必須使用 myqsl_num_rows() 函數。

相關函數:mysql_num_rows()

mysql_db_query() 函數

resource mysql_db_query(string database, string query, resource [link_identifier]);

mysql_db_query 與 mysql_query() 最大的不同在於,mysql_db_query() 能在特定的資料庫裡執行 SQL 敘述。

成功的話傳回一個 result 指標,否則傳回 false。

相關函數:mysql_connect(), mysql_query()

mysql_drop_db() 函數

bool mysql_drop_db(string database_name, resource [link_identifier]);

刪除資料庫。成功傳回 true,失敗傳回 false。

相關函數: mysql_create_db()

mysql_errno() 函數

int mysql_errno(resource [link_identifier]);

傳回錯誤代碼,可配合 mysql_error() 函數傳回錯誤訊息。例如:

<?php mysql_connect("localhost", "admin", "test"); ?>
<font size=+1 color="#FF0000">(</font> <?php echo mysql_errno(). " --> ".mysql_error(); ?> <font size=+1 color="#FF0000">)</font>

相關函數:mysql_error()

mysql_error() 函數

string mysql_error(resource [link_identifier]);

傳回錯誤訊息。

相關函數:mysql_errno()

mysql_fetch_array() 函數

array mysql_fetch_array(resource result, int [result_type]);

取得資料錄,傳回由 result_type 所指定的型式。失敗或是沒有資料則傳回 false。

result_type 所指定的型式有 3 種:

1. MYSQL_ASSOC - associative array
2. MYSQL_NUM - enumerated array
3. MYSQL_BOTH - associative array 與 enumerated array

若是指定 MYSQL_ASSOC,則傳回的 associative array 元素名稱 (key) 則是資料表裡的欄位名稱。例如資料表裡的 name 欄位存放一筆 'paul' 的資料,則傳回的 associative array 讀取資料的方式為:

$rows = mysql_fetch_array($result); echo $rows['name']; //輸出 paul

如果有一樣的 key,則後面的元素會被忽略。如果有名稱一樣的 key,建議使用 mysql_fetch_row() 函數來讀取資料錄,或者設定 mysql_fetch_array() 傳回以數字為 key 的陣列。MYSQL_NUM 指定 mysql_fetch_array() 傳回以數字為 key 的 array。

請看底下的範例:

$rows = mysql_fetch_array($result, MYSQL_NUM); echo $rows[1] . $rows[2];

mysql_fetch_array() 雖然提供較複雜的功能,但速度並不比 mysql_fetch_row() 慢,因此我們可以放心地使用。

範例:

<?php
mysql_connect($host, $user, $password); $result = mysql_db_query("student","select * from table"); while($row = mysql_fetch_array($result)) { echo $row["fullname"]; echo $row["grade"]; } mysql_free_result($result);
?>

相關函數:mysql_fetch_row(), mysql_fetch_assoc()

mysql_fetch_field() 函數

object mysql_fetch_field(resource result, int [field_offset]);

取得欄位 (field) 的資訊,傳回包含取得資訊的物件。mysql_fetch_field() 共接受2 個參數:

1. 第 1 個參數為指向 result 的指標,該指標指向一個資料錄 (row)。
2. 第 2 個參數為該資料錄的欄位位置,如果沒有指定要取得資訊的欄位位置 (field_offset),則內定傳回下一個尚未取回資訊的欄位。

傳回的物件包含底下這些屬性 (properties):

1. name 欄位名稱
2. table 欄位所在的資料表名稱
3. max_length 該欄位的最大長度
4. not_null 如果該欄位不能接受 NULL,則為 1。
5. primary_key 如果該欄位為 primary key,則為 1。
6. unique_key 如果該欄位為 unique key,則為 1。
7. multiple_key 如果該欄位不是 unique key,則為 1。
8. numeric 如果該欄位為數字型態,則為 1。
9. blob 如果該欄位為 BLOB 型態,則為 1。
10. type 該欄位的資料型態。型態名稱為一個字串,該字串則是資料型態的名稱,資料型態名稱與第七章介紹的型態名稱一樣。
11. unsigned 如果該欄位為 unsigned 屬性,則為 1。
12. zerofill 如果該欄位為 zerofill 屬性,則為 1。

相關函數:mysql_field_seek()

mysql_fetch_lengths() 函數

array mysql_fetch_lengths(resource result);

mysql_fetch_lengths() 函數用來取得最後一筆由 mysql_fetch_* 取回的各欄位資料長度。mysql_fetch_lengths() 傳回一個 enumerated array (key 由 0 開始),每個元素分開存放每個欄位的資料長度。失敗的話則傳回 false。

相關函數:mysql_fetch_row().

mysql_fetch_object() 函數

object mysql_fetch_object(resource result, int [result_typ]);

取回 result 指標所指的資料錄,以物件的型式傳回資料。失敗的話傳回 false。

result_typ 參數可指定 3 個常數值如下:

1. MYSQL_ASSOC
2. MYSQL_NUM
3. MYSQL_BOTH。

由於傳回的是物件型態,因此資料要以屬性的方式讀取,mysql_fetch_object() 比起 mysql_fetch_array() 或 mysql_fetch_row() 來得好用,而且速度也不慢,建議讀者可大量使用。

範例:

<?php while($row = mysql_fetch_object($result)) { echo $row->user_id; echo $row->fullname; } ?>

相關函數:mysql_fetch_array(), mysql_fetch_row()

mysql_fetch_row() 函數

array mysql_fetch_row(resource result);

取回 result 指標所指的資料錄,以 enumerated array 型式存放傳回的資料,key 由 0 開始。失敗的話傳回 false。如果成功取得資料,result 指標自動指向下一筆符合條件的資料錄。

mysql_fetch_row() 與 mysql_fetch_array(result, MYSQL_NUM) 的功能相同。

相關函數:mysql_fetch_array(), mysql_fetch_assoc(), mysql_fetch_object(), mysql_data_seek(), mysql_fetch_lengths(), mysql_result()

mysql_fetch_assoc() 函數

array mysql_fetch_assoc ( resource result);

取回 result 指標所指的資料錄,以 associative array 型式存放傳回的資料。失敗或是沒有資料的話則傳回 false。

mysql_fetch_assoc() 與 mysql_fetch_array(result, MYSQL_ASSOC) 的功能相同。本函數必須在 PHP 4.0.3 與之後的版本才能使用。

mysql_field_name() 函數

string mysql_field_name(resource result, int field_index);

取得特定欄位的名稱。要特別注意的是 field_index 參數,field_index 參數指的是第幾個欄位。例如:

mysql_field_name($result, 3)

會傳回第 3 個欄位的名稱。

mysql_field_seek() 函數

int mysql_field_seek(resource result, int field_offset);

移動 result 指標到指定的位置。field_offset 指的是 result 所指資料錄的位置。

相關函數:mysql_fetch_field()

mysql_field_table() 函數

string mysql_field_table(resource result, int field_offset);

傳回欄位所屬的資料表名稱。

mysql_field_type() 函數

string mysql_field_type(resource result, int field_offset);

傳回欄位的資料型態。

mysql_field_flags() 函數

string mysql_field_flags(resource result, int field_offset);

傳回欄位的屬性,包括:

1. not_null
2. primary_key
3. unique_key
4. multiple_key
5. blob
6. unsigned
7. zerofill
8. binary
9. enum
10. auto_increment
11. timestamp

如果一個欄位包含多種屬性,則每個屬性間以空白隔開。

mysql_field_len() 函數

int mysql_field_len(resource result, int field_offset);

傳回欄位的長度。

mysql_insert_id() 函數

int mysql_insert_id(resource [link_identifier]);

取得由最近 INSERT 一個 AUTO_INCREMENT 欄位所產生的 id。

mysql_list_fields() 函數

resource mysql_list_fields(string database_name, string table_name, resource [link_identifier]);

取得資料表的資訊,傳回一個指向傳回資訊的指標,可利用 mysql_field_flags()、mysql_field_len()、mysql_field_name() 與 mysql_field_type() 取得這些資料。失敗的話則傳回 -1。

mysql_list_dbs() 函數

resource mysql_list_dbs(resource [link_identifier] );

查詢目前可使用的資料庫名稱,傳回一個指向結果的指標,利用 mysql_tablename() 函數來讀取各筆資料。

相關函數:mysql_db_name()

mysql_list_tables() 函數

resource mysql_list_tables(string database, resource [link_identifier] );

查詢指定資料庫裡所有的資料表,傳回一個 result 指標,可利用 mysql_tablename() 函數來讀取各筆資料。

mysql_num_fields() 函數

int mysql_num_fields(resource result);

取得 result 裡的欄位個數。

相關函數:mysql_db_query(), mysql_query(), mysql_fetch_field(), mysql_num_rows()

mysql_num_rows() 函數

int mysql_num_rows(resource result);

取得 result 的資料筆數。

相關函數:mysql_db_query(), mysql_query(), mysql_fetch_row()

mysql_result() 函數

mixed mysql_result(resource result, int row, mixed field);

取得 result 的資料。mysql_result() 只能讀取 1 個欄位的資料,所指定的欄位可以是欄位位置的偏移值 (field's offset)、欄位名稱或是 field_name.table_name 的型式,但其中以傳入欄位位置偏移值的執行速度最快。

如果要在大量的資料裡讀取 1 個欄位的資料,則最好使用 mysql_fetch_row()、mysql_fetch_array() 或 mysql_fetch_object() 函數,因為這幾個函數的速度都比 mysql_result() 來得快。

mysql_unbuffered_query() 函數

resource mysql_unbuffered_query ( string query [, resource link_identifier]);

mysql_unbuffered_query() 的功能與 mysql_query() 相同。唯一的差別在於,mysql_unbuffered_query() 並不會事先取得資料庫裡的資料,而 mysql_query() 則是會先取得所有的資料。

mysql_unbuffered_query() 函數只是先把 SQL 命令傳送給 MySQL server,但是不會事先取得資料 (result sets)。利用 mysql_unbuffered_query() 可以讓我們在需要資料列時 (result rows) 才利用相關函數向 MySQL server 取得。

請看底下的範例:

<?php $res1 = mysql_unbuffered_query("SELECT * FROM mouse_types"); $res2 = mysql_unbuffered_query("SELECT * FROM keyboard_types"); $res3 = mysql_unbuffered_query("SELECT * FROM monitor_types");
if ($search == "mouse") { $row = mysql_fetch_row($res1); //… } elseif ($search == "keyboard") { $row = mysql_fetch_row($res2); //… } elseif ($search == "monitor") { $row = mysql_fetch_row($res3); //… } else { echo "Invalid search paramemter!"; } ?>

在這個範例裡,$res1、$res2 與 $res3 並不會真的指向由資料庫取得的資料,而是當程式呼叫 mysql_fetch_row() 函數後,才會做資料下載的動作。這種寫法好處是可以節省相當多寶貴的記憶體,而不會浪費不必要的記憶體來存放由資料庫裡取得的資料。

本函數只能在 PHP 4.0.6 與之後的版本使用。

mysql_tablename() 函數

string mysql_tablename(resource result, int i);

取得資料表名稱。result 是由 mysql_list_tables() 所傳回來的指標,i 則是資料表的 key,i 的個數可以利用 mysql_num_rows() 取得,即 0 <= i < mysql_num_rows()。例如:

<?php mysql_connect (); $result = mysql_list_tables ("book"); $i = 0; while ($i < mysql_num_rows ($result)) { $tb_names[$i] = mysql_tablename ($result, $i); echo $tb_names[$i] . "
"; $i++; } ?>

mysql_escape_string() 函數

string mysql_escape_string(string unescaped_string);

mysql_escape_string() 函數會將 unescaped_string 字串做跳脫字元 (escaped character) 處理後,傳回 1 個跳脫字元處理後的字串。

mysql_escape_string() 函數並不會處理 '%' 與 '_' 字元。我們可以將 SQL 語法先經過 mysql_escape_string() 處理後再傳給 mysql_query() 函數使用,以增加安全性。

本函數只能在 PHP 4.0.3 與之後的版本使用。

mysql_real_escape_string() 函數

string mysql_real_escape_string(string unescaped_string [, resource link_identifier]);

mysql_real_escape_string() 函數會根據系統目前的字元集 (charset - character set) 設定,將 unescaped_string 字串裡的特殊字元做跳脫字元 (escaped character) 處理後,傳回 1 個跳脫字元處理後的字串。

要注意mysql_real_escape_string() 函數並不會對 % 與 _ 做跳脫處理。本函數只能在 PHP 4.3.0 與之後的版本使用。

mysql_get_client_info() 函數

string mysql_get_client_info (void);

mysql_get_client_info() 函數傳回 MySQL client 的函式庫版本資訊。

範例:

<?php echo mysql_get_client_info(); ?>

本函數只能在 PHP 4.0.5 與之後的版本使用。

mysql_get_host_info() 函數

string mysql_get_host_info([resource link_identifier]);

mysql_get_host_info() 函數傳回 link_identifier 指定的 MySQL 連線型態,包含 server host name 與 connection 型態。若未指定 link_identifier 參數,則以最後一個 MySQL 連線為主。

範例:

<?php echo mysql_get_host_info(); ?>

輸出結果範例:

Localhost via UNIX socket

本函數只能在 PHP 4.0.5 與之後的版本使用。

mysql_get_proto_info() 函數

int mysql_get_proto_info([resource link_identifier]);

mysql_get_proto_info() 函數傳回 link_identifier 指定的 MySQL 連線通訊協定 (protocol) 相關資訊。若未指定 link_identifier 參數,則以最後一個 MySQL 連線為主。

範例:

<?php echo mysql_get_proto_info(); ?>

輸出結果範例:

10

本函數只能在 PHP 4.0.5 與之後的版本使用。

mysql_get_server_info() 函數

int mysql_get_server_info([resource link_identifier]);

mysql_get_server_info() 函數傳回 link_identifier 指定連線的 MySQL server 版本資訊。若未指定 link_identifier 參數,則以最後一個 MySQL 連線為主。

範例:

<?php echo mysql_get_server_info(); ?>

輸出結果範例:

4.0.1-alpha

本函數只能在 PHP 4.0.5 與之後的版本使用。

mysql_client_encoding() 函數

int mysql_client_encoding( [resource link_identifier]);

mysql_client_encoding() 函數傳回目前所使用的字元集 (character set) 名稱,例如:

$charset = mysql_client_encoding($link); echo $charset; //輸出 "latin1" … 等,視系統設定而定。

本函數只能在 PHP 4.3.0 與之後的版本使用。

mysql_info() 函數

string mysql_info( [resource link_identifier]);

mysql_info() 函數傳回最後一次 MySQL 資料庫的 query 詳細結果,利用 mysql_info() 函數可以得知 SQL 語法操作的最後詳細結果。

本函數只能在 PHP 4.3.0 與之後的版本使用。

mysql_ping() 函數

bool mysql_ping([resource link_identifier]);

mysql_ping() 會 ping 所指定的 MySQL server 連線,若無法 ping 成功,則會試著自動重新連線。

本函數只能在 PHP 4.3.0 與之後的版本使用。

mysql_stat() 函數

string mysql_stat([resource link_identifier]);

mysql_stat() 目前可以傳回部份的系統狀態資訊,更多的系統狀態可以使用 SHOW STATUS 命令取得。

本函數只能在 PHP 4.3.0 與之後的版本使用。

mysql_thread_id() 函數

int mysql_thread_id([resource link_identifier]);

傳回與 MySQL 連線的 thread id,mysql_thread_id() 傳回的 thread id 僅供參考,不能做為其它的特殊用途。

本函數只能在 PHP 4.3.0 與之後的版本使用。

--jollen

版權聲明

請參閱頁面底部的 CC 授權條款。此外,Jollen's PHP 專欄禁止引用、修改與轉貼,如果您想與朋友分享的話,我們只允許網站連結(hyperlink)的形式。

評論 (1)

<?php

$sql = "SELECT * FROM `jollen` WHERE 1";

// 1. connect to mysql server
$link = mysql_connect("localhost", "phpuser", "123456");

// 2. choose database
$ret = mysql_select_db("phplab", $link);
if (!$ret) {
echo "<h1>MySQL connection failed.</h1>";
exit;
}

// 3. query table
$res = mysql_query($sql, $link);

// 4. parse resource
while ($obj = mysql_fetch_object($res)) {
echo "";
echo "Name: {$obj->nickname}<br />";
echo "Email: {$obj->email}<br />";
echo "URL: {$obj->url}<br />";
echo "Message: <pre>{$obj->message}</pre><br />";
echo "?</p>";
}

?>

發表一個評論

(如果你此前從未在此 Blog 上發表過評論,則你的評論必須在 Blog 主人驗證後才能顯示,請你耐心等候。)

| Top | Jollen's Forum |
Jollen's PHP 專欄採用 Attribution-NonCommercial-NoDerivs 2.5 授權條款
Copyright(c) 2001-2007,2010 www.jollen.org