Ever wondered what the difference between mysql_fetch_array and mysql_fetch_assoc is? What about mysql_fetch_row? I’ve always used mysql_fetch_array, but, for debugging purposes, here’s what they all do:

  • mysql_fetch_assoc returns an associative array of your field names eg $row['field_name_here']
  • mysql_fetch_row returns a numeric array eg $row[0] returns the first column, $row[1] returns the second column etc. – not a good idea as it makes the code less readable, and if the column order changes, or another column is inserted, the order will break.
  • mysql_fetch_array returns both an associative and a numeric array – supposedly slower.

mysql_fetch_array() returns essentially two arrays, one with a numeric index and one with an associative based key index. Thus, using mysql_fetch_array() without specifying which method you want (either MYSQL_NUM or MYSQL_ASSOC), always returns a double array, which is considerably more inefficient as compared to mysql_fetch_row() or mysql_fetch_assoc().


Recommend This Story!
Save to Delicious | Get the vote5 plugin!
Tags: , ,


Like Our Site? Follow Us!



Read More on the Same Subject


Read before commenting! We welcome constructive comments and allow any that meet our common sense criteria. This means being respectful and polite to others. It means providing helpful information that contributes to a story or discussion. It means leaving links only that substantially add further to a discussion. Comments using foul language, being disrespectful to others or otherwise violating what we believe are common sense standards of discussion will be deleted.

  • http://www.sutanaryan.com/ Ryan Sutana

    so you can recommend that mysql_fetch_assoc is the best way to use? as it is not slow?

    • http://getbutterfly.com/ Ciprian

      Yes. It’s faster with a few milliseconds than mysql_fetch_array and equal in speed with mysql_fetch_row as it does the same thing, but, you know, every millisecond counts.