Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql fetch assoc VS mysql fetch array

Tags:

php

mysql

fetch

Below are two methods commonly used in most php codes for fetch mysql data .

  1. mysql_fetch_array()
  2. mysql_fetch_assoc()

Now, I have a big database. For fetching data in loops (while) what's faster and better ?

I found this benchmark.

Which is your choice?

like image 498
Saimon Avazian Avatar asked Mar 02 '12 21:03

Saimon Avazian


People also ask

What is difference between Mysql_fetch_object and mysql_fetch_array?

The mysqli_fetch_object() function returns objects from the database, whereas mysqli_fetch_array() function delivers an array of results. This will allow field names to be used to access the data.

What is difference between mysql_fetch_array and mysql_fetch_assoc?

Difference: mysql_fetch_assoc() will always assing a non-secuencial key (like "color" and not a number). mysql_fetch_array() will assing a number key if there are not "word" key (0 and not "color" if there are not "color") but, you can choose to assing of key with a parameter...

What is the difference between mysqli_fetch_assoc and mysqli_fetch_array?

The major difference between mysqli_fetch_assoc and mysqli_fetch_array is the output format of result data. mysqli_fetch_assoc returns data in an associative array and mysqli_fetch_array returns data in a numeric array and/or in an associative array.

What is difference between mysql_fetch_array and mysql_fetch_row?

mysqli_fetch_array() is an extended version of the mysqli_fetch_row() function. In addition to storing the data in the numeric indices of the result array, the mysqli_fetch_array() function can also store the data in associative indices, using the field names of the result set as keys.


1 Answers

It depends on how your tables are setup:

mysql_fetch_array() essentially returns two arrays one with numeric index, one with associative string index.

So using mysql_fetch_array() without specifying MYSQL_ASSOC or MYSQL_NUM, or by specifying MYSQL_BOTH will return two arrays (basically what mysql_fetch_assoc() and mysql_fetch_row() would return) so mysql_fetch_assoc() is faster.

If you have your table setup right and query written properly mysql_fetch_assoc() is the way to go, code readability wise $result['username'] is easier to understand than $result[0].

like image 125
The Coder Avatar answered Sep 22 '22 14:09

The Coder