Mysqli fetch_assoc vs fetch_array

When I return one row from the table to collect the results that I usually use, for example:

$info = $result->fetch_assoc(); 

What is the difference between this and:

 $info = $result->fetch_array(); 

Is there a reason to use one over the other when returning only one row, or is it just personal preference?

+9
php
source share
4 answers

All about performance

fetch_array() returns a single array with numeric keys and associative strings (column names), so here you can use $row['column_name'] or $row[0]

Where an array of strings with an indexed string will be returned as fetch_assoc() and there is no number array, so you will not be able to use numeric keys such as $row[0] .

Thus, the latter is better in performance compared to fetch_array() and, obviously, the use of named indexes is much better compared to numerical indexes.

+19
source share

fetch_array returns an indexed value. But Fetch_assoc just returns a value.

for example fetch_array returns

 [0] => 11 [id] => 11 [1] => 2014-12-29 [date] => 2014-12-29 

here the location of array 0 contains 11, also this location name is 'id'.

same as fetch_assoc will return

 [id] => 11 [date] => 2014-12-29 

means it just returns a value.

+8
source share

(PHP 5) From: http://php.net/manual/en/mysqli-result.fetch-array.php

mysqli_result :: fetch_array - mysqli_fetch_array - select the result string as an associative, numeric array, or both

Thus, fetch_array() and fetch_assoc() can be essentially equivalent calls. In terms of performance, I recommend using any call that results in more readable code, and only cares about performance after you have clearly defined the call as a performance problem. Most likely, you have more serious performance problems when you least expect it.

+2
source share

fetch_array will give you key / value pairs and indexes, where since fetch_assoc will only give you key / value pairs, but not indexes. For example:

 //fetch_array output be like: [name]=>'someName' [0]=>'someName' [email]=>'some@some.com' [1]=>'some@some.com' //fetch_assoc output be like [name]=>'someName' [email]=>'some@some.com' 
0
source share

All Articles