mysql_fetch_array(),mysql_fetch_assoc() and mysql_fetch_row()

Summary

Understanding the PHP MySQL functions for fetching query results is crucial for efficient data handling. mysql_fetch_array() offers flexibility, allowing results to be returned as associative arrays (MYSQL_ASSOC), numerically indexed arrays (MYSQL_NUM), or both (MYSQL_BOTH). mysql_fetch_assoc() specifically retrieves rows as associative arrays, while mysql_fetch_row() returns them as numerically indexed arrays, both mirroring specific mysql_fetch_array() modes. Developers should note that mysql_fetch_row() is typically the fastest option, and mysql_fetch_object() provides similar functionality but returns an object instead of an array.

In PHP MySQL mannual. There are three functions which need to be clarified for some users who may get confused when choosing which one to use to get the result.

mysql_fetch_array() : by seeting the different parameters, there are three ways to return the result set. MYSQL_ASSOC(Result set with field names as the associative indexs.It means you can use the field name as the index to get value of the specified cell). MYSQL_NUM(Result set with field names as the number indices). Or MYSQL_BOTH(you can use both modes to get value of a specified cell).

mysql_fetch_assoc():Result set with field names as the associative indexs.It means you can use the field name as the index to get value of the specified cell. It similars to mysql_fetch_array() with MYSQL_ASSOC speficied.

mysql_fetch_row():Result set with field names as the number indices.It similars to mysql_fetch_array() with MYSQL_NUM speficied.

Among these 3 functions, usually mysql_fetch_row() is the fastest one.

Note : mysql_fetch_object() has similar functions to the three functions mentioned above with the return value object instead of array.

PHP COMPARISON MYSQL RESULTSET MYSQL_FET

  RELATED

  COMMENTS

0

No comment for this article.