php資料庫操作——獲取資料查詢結果
阿新 • • 發佈:2018-12-31
先進行連線,然後執行SQL語句,獲取資料的結果集。PHP有多個函式可以獲取資料的結果集,最常使用mysql_fetch_array,通過設定引數更改行資料的下標,數字索引的下標和欄位名關聯索引的下標。
$sql = "select * from user limit 1"; $result = mysql_query($sql); $row = mysql_fetch_array($result);
可以通過設定引數MYSQL_NUM只獲取數字索引陣列,等同於mysql_fetch_row函式,如果設定引數為MYSQL_ASSOC則只獲取關聯索引陣列,等同於mysql_fetch_assoc函式。
$row = mysql_fetch_row($result); $row = mysql_fetch_array($result, MYSQL_NUM); //這兩個方法獲取的資料是一樣的
$row = mysql_fetch_assoc($result); $row = mysql_fetch_array($result, MYSQL_ASSOC);
如果要獲取資料集中的所有資料,我們通過迴圈來遍歷整個結果集。
$data = array(); while ($row = mysql_fetch_array($result)) { $data[] = $row; }