When I'm returning one row from a table, to gather the results I usually use e.g.:
$info = $result->fetch_assoc();
What is the difference between that 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?
fetch_array returns value with indexing. But Fetch_assoc just returns the the value. here array location 0 contains 11 also this location name is 'id'. means just returns the value.
The fetch_assoc() / mysqli_fetch_assoc() function fetches a result row as an associative array.
The fetch_array() / mysqli_fetch_array() function fetches a result row as an associative array, a numeric array, or both. Note: Fieldnames returned from this function are case-sensitive.
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.
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.
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