webmaster@my.site>站長";
}
$row=mysql_fetch_row($res);
echo"未來您使用的號碼為:
".$row[0];
?
>
參考
mysql_close()
mysql_errno
傳回前一MySQL操作的錯誤訊息的代碼。
前一MySQL操作不含mysql_error()andmysql_errno()二函數。
語法:
intmysql_errno([resourcelink_identifier])
傳回值:
前一MySQL函數作業錯誤時,傳回錯誤訊息的代碼。
前一MySQL函數作業正常時,傳回0。
使用範例
php
mysql_connect("localhost","mysql_user","mysql_password");
mysql_select_db("nonexistentdb");
echomysql_errno().":
".mysql_error()."\n";
mysql_select_db("kossu");
mysql_query("SELECT*FROMnonexistenttable");
echomysql_errno().":
".mysql_error()."\n";
?
>
上述案例的輸出為:
1049:
Unknowndatabase'nonexistentdb'
1146:
Table'kossu.nonexistenttable'doesn'texist
mysql_error
傳回前MySQL工作的錯誤訊息內容。
前一MySQL操作不含mysql_error()andmysql_errno()二函數。
語法:
stringmysql_error([resourcelink_identifier])
傳回值:
前一MySQL函數作業錯誤時,傳回錯誤訊息的內容。
前一MySQL函數作業正常時,傳回空字串。
使用範例
php
mysql_connect("localhost","mysql_user","mysql_password");
mysql_select_db("nonexistentdb");
echomysql_errno().":
".mysql_error()."\n";
mysql_select_db("kossu");
mysql_query("SELECT*FROMnonexistenttable");
echomysql_errno().":
".mysql_error()."\n";
?
>
上述案例的輸出為:
1049:
Unknowndatabase'nonexistentdb'
1146:
Table'kossu.nonexistenttable'doesn'texist
mysql_fetch_array
傳回陣列資料。
以資料庫的欄位名稱為索引值。
語法:
arraymysql_fetch_array(intresult,int[result_typ]);
傳回值:
陣列
內容說明
本函式用來將查詢結果result拆到陣列變數中。
若result沒有資料,則傳回false值。
而本函式可以說是mysql_fetch_row()的加強函式,除可以將傳回列及數字索引放入陣列之外,還可以將文字索引放入陣列中。
若是好幾個傳回欄位都是相同的文字名稱,則最後一個置入的欄位有效,解決方法是使用數字索引或者為這些同名的欄位(column)取別名(alias)。
值得注意的是使用本函式的處理速度其實不會比mysql_fetch_row()函式慢,要用哪個函式還是看使用的需求決定。
參數result_typ是一個常數值,有以下幾種常數MYSQL_ASSOC、MYSQL_NUM與MYSQL_BOTH。
使用範例
php
mysql_connect($host,$user,$password);
$result=mysql_db_query("database","select*fromtable");
while($row=mysql_fetch_array($result)){
echo$row["user_id"];
echo$row["fullname"];
}
mysql_free_result($result);
?
>
php
$link=mysql_connect("localhost","mysql_user","mysql_password");
mysql_select_db("database",$link);
$result=mysql_query("SELECT*FROMtable1",$link);
$rows=mysql_fetch_array($result);
echo"$rows[“name”],$rows[“sex”]\n";
?
>
mysql_fetch_row
傳回單列的各欄位。
以數字為索引值並由0開始起。
語法:
arraymysql_fetch_row(intresult);
傳回值:
陣列(array)
內容說明
本函式用來將查詢結果result之單列拆到陣列變數中。
陣列的索引是數字索引,第一個的索引值是0。
若result沒有資料,則傳回false值。
使用範例
php
mysql_connect($host,$user,$password);
$result=mysql_db_query("database","select*fromtable");
while($row=mysql_fetch_row($result)){
echo$row[0];
echo$row[1];
}
mysql_free_result($result);
?
>
php
$link=mysql_connect("localhost","mysql_user","mysql_password");
mysql_select_db("database",$link);
$result=mysql_query("SELECT*FROMtable1",$link);
$num_rows=mysql_fetch_rows($result);
echo"$rows[0],$rows[1]\n";
?
>
參考
mysql_fetch_array()mysql_fetch_object()mysql_data_seek()mysql_fetch_lengths()mysql_result()
mysql_free_result
釋放工作結果區的記憶體空間。
語法:
boolmysql_free_result(resourceresult)
釋放和result識別相關的記憶體空間。
傳回值:
布耳變數值(Boolean)
成功回傳TRUE
失敗回傳FALSE
內容說明
當該程式(script)執行完畢,這些空間會自動的解除。
除非使用大量的記憶,否則不需執行本mysql_free_result()函數。
mysql_insert_id
傳回最後一次使用INSERT指令的ID。
語法:
intmysql_insert_id(int[link_identifier]);
傳回值:
1.0:
若前一SQL查詢未產生AUTO_INCREMENT值時,故必須碓定使用本mysql_insert_id()函數,緊接著產生AUTO_INCREMENT值函數之後。
2.上筆的插入記錄的索引值:
為前一INSERT查詢,對定義為AUTO_INCREMENT欄位所產生的值。
內容說明
本函式可以得到最後一次使用INSERT到MySQL資料庫的執行ID。
sleibowitz@btcwcu.org(13-May-1999)指出在PHP3.0.7版用REPLACE也和使用INSERT一樣,可以使用本函式獲得ID。
若未指定通路識別(link_identifier)時,假設為最後建立的通道。
使用範例
php
mysql_connect("localhost","mysql_user","mysql_password")or
die("couldnotconnect");
mysql_select_db("mydb");
mysql_query("INSERTINTOmytable(product)values('kossu')");
printf("Lastinsertedrecordhasid%d\n",mysql_insert_id());
?
>
mysql_list_fields
列出指定資料表的欄位(field)。
語法:
intmysql_list_fields(stringdatabase_name,stringtable_name,int[link_identifier]);
傳回值:
整數
內容說明
本函式可以得到指定的資料表的所有欄位。
傳回的欄位資訊可以供mysql_field_flags()、mysql_field_len()、mysql_field_name()及mysql_field_type()等函式使用。
若有錯誤則傳回-1。
使用範例
mysql_num_rows
取得傳回列的數目。
語法:
intmysql_num_rows(intresult);
傳回值:
整數
內容說明
本函式可以得到傳回列的數目。
常用以動態的產生,資料表的內容數。
使用範例
查詢在table1內的資料筆數。
php
$link=mysql_connect("localhost","mysql_user","mysql_password");
mysql_select_db("database",$link);
$result=mysql_query("SELECT*FROMtable1",$link);
$num_rows=mysql_num_rows($result);
echo"$num_rowsRows\n";
?
>
mysql_query
送出一個query字串,給MySQL對現用的資料庫執行作業。
語法:
resourcemysql_query(stringquery[,resourcelink_identifier[,intresult_mode]])
傳回值:
1.當query指令是UPDATE、INSERT及DELETE時,則傳回的值可能是true或者false;
2.當query指令是SELECT,SHOW,EXPLAINorDESCRIBE等陳述時,則傳回一個指標用以指向工作的結果區(為一二維表格)。
指標在mysql的函數語法格式以【resourceresult】此resource表變數的型式。
3.joey@(09-Feb-1999)指出,當傳回false時,並不是執行成功但無傳回值,而是查詢的字串有錯誤。
4.傳回非false的值,僅代表該查詢的語法是合乎文法,但不保證語意是正確的。
內容說明
本函式送出query字串供MySQL做相關的處理或者執行。
若未指定通路識別(link_identifier)時,則程式會自動假設為最後建立的通道。
參數result_mode為一選項參數,其值有:
1.MYSQL_USE_RESULT
2.MYSQL_STORE_RESULT
預設值為:
MYSQL_STORE_RESULT,即結果(result)是暫存在緩衝區(buffered)內中。
.
參見mysql_unbuffered_query()的另一種相反的作法。
注意:
SQL查詢字串不可以分號(;)結束。
使用範例
查詢字串的語法錯誤,傳回FALSE。
$result=mysql_query("SELECT*WHERE1=1")
ordie("Invalidquery");
?
>
例二:
若欄位(my_col)不為資料表(my_tbl)的欄位,則回傳FALSE。
php
$result=mysql_query("SELECTmy_colFROMmy_tbl")
ordie("Invalidquery");
?
>
參考
mysql_select_db()mysql_connect()
mysql_select_db
從MySQL伺服站中,選擇一個資料庫。
語法:
boolmysql_select_db(stringdatabase_name[,resourcelink_identifier])
傳回值:
布耳變數值(Boolean)
成功回傳TRUE
失敗回傳FALSE
內容說明
本函式mysql_select_db()設定在連結識別(linkidentifier)指定的伺服器上的現用的資料庫,以供之後的資料查詢作業(query)處理。
成功傳回true,失敗則傳回false。
如果沒有指定連結識別(linkidentifier),則假設為最後打開的連結【mysql_connect()】。
使用範例
參考
mysql_connect()mysql_pconnect()mysql_query()