PHP中mysql_fetch_array()和mysql_fetch_row()的區別

來源:互聯網
上載者:User

 最近在做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']一樣

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.