I see comparisons between mysqli_fetch_array()
and mysqli_fetch_all()
that say that with mysqli_fetch_all()
it will take more memory and I will have to iterate over the array.
mysqli_fetch_all()
is one call to mysqli
but mysqli_fetch_array()
is one call per row, for example 100 calls.
I don't know how the mysqli processing works: is calling mysqli_fetch_array()
really more efficient when you also take the number of calls into account?
(I already understand that the returned data can be associative arrays or not)
From PHP's page on
mysql_fetch_all()
:It has nothing to do with whatever efficiency. It's all about usability only.
fetch_all()
is a thing that is called a "syntax sugar" - a shorthand to automate a frequently performed operation. It can be easily implemented as a userland function:Thus you can tell use cases for these functions:
fetch_all()
have to be used if you need an array, consists of all the returned rows, that will be used elsewhere.fetch_assoc()
in a loop have to be used if you're going to process all the rows one by one right in place.As simple as that.
These functions bear different purpose and thus there is no point in comparing them.
Note that PDO is tenfold more sweet in terms of syntax sugar, as it's
fetchAll()
function can return data in dozens different formats