PHP Coding Tips

Friday, August 10, 2012

mysql_fetch_array and mysql_fetch_assoc difference

Many beginners in php user mysql_fetch_array after using mysql_query. This taking a row from mysql result set and put it's resulting data to an array format. By first look, this work same as mysql_fetch_assoc but the difference is in way that data is put. Assoc is creating less indexes, because it uses only those indexes thar are column names. Mysql_fetch_array will also create values in numered indexes starting from 0, representing next columns.$q = mysql_query("SELECT id,val FROM table"); // This create indexes 0,1,id,val: print_r( mysql_fetch_array( $q ) ); // This create indexes id,val: print_r( mysql_fetch_assoc( $q ) );So if you are not using indexes like 0, 1 when getting data from your query, but using only associative array keys - don't use mysql_fetch_array to gain performance.

No comments:

Post a Comment