Below are two methods commonly used in most php codes for fetch mysql data .
mysql_fetch_array()
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?
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.
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...
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.
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.
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]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With