最近在做PHP與資料庫互動的project,急於求成,模仿了下例子就開始動手,結果誤把mysql_fetch_array寫成了mysql_fetch_row,囧事來了,發現返回的數組居然是index=>value的形式,而明明記得是field name=>value的哈,查手冊才明白。
1. mysql_fetch_array的函數原型是
array mysql_fetch_array ( resource $result [, int $result_type= MYSQL_BOTH ] )
Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.
The type of returned array depends on how result_type is defined. By using MYSQL_BOTH (default), you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).
2. 這就很明白了,若設定不同的參數,mysql_fetch_array()可以做到mysql_fetch_row()和mysql_fetch_assoc()的功能,而後兩個函數就只接受第一個參數,返回的分別是associative=>value, index=>value的數組
3.MYSQL_BOTH是什麼效果呢,測試了下
Sql: select firstname,lastname from table where id="c01";
$array=mysql_fetch_array($result,MYSQL_BOTH);
$array[0]和$array['firstname']一樣