Mysqli fetch_assoc vs fetch_array
It's all about performance
fetch_array()
returns one array with both numeric keys, and associative strings (column names), so here you can either use $row['column_name']
or $row[0]
Where as fetch_assoc()
will return string indexed key array and no numeric array so you won't have an option here of using numeric keys like .$row[0]
So the latter one is better in performance compared to fetch_array()
and obviously using named indexes is far better compared to numeric indexes.
fetch_array returns value with indexing. But Fetch_assoc just returns the the value.
for example fetch_array returns
[0] => 11
[id] => 11
[1] => 2014-12-29
[date] => 2014-12-29
here array location 0 contains 11 also this location name is 'id'.
same things fetch_assoc will returns
[id] => 11
[date] => 2014-12-29
means just returns the value.
(PHP 5) From: http://php.net/manual/en/mysqli-result.fetch-array.php
mysqli_result::fetch_array -- mysqli_fetch_array — Fetch a result row as an associative, a numeric array, or both
So effectively, fetch_array()
and fetch_assoc()
can be essentially equivalent calls. As for performance, I recommend using whichever call that results in more readable code and only be concerned about the performance after you've clearly identified the call as a performance issue. It's more likely that you've got more serious performance issues where you least expect them to be.
fetch_array
will give you key/value pairs and with indexes, where as fetch_assoc
will give you only the key/value pairs but not with indexes. For example:
//fetch_array output be like:
[name]=>'someName'
[0]=>'someName'
[email]=>'[email protected]'
[1]=>'[email protected]'
//fetch_assoc output be like
[name]=>'someName'
[email]=>'[email protected]'