3D-kreativ 3D-kreativ - 1 year ago 65
PHP Question

Read data from mysqli_fetch_array like a multidimensional array?

When I use

I get an array, but how do I read the values? Is a While-Loop the only option, or can I pick any value from a row and column like a multidimensional array with index like
row[0] column [3]

Answer Source

while loop fetches you a row per iteration.

You can get all rows as multidimensional array with mysqli_fetch_all

After that you can use pick your values with [rowNum][colNum]

But beware when your result has lot of rows - array can be very big and cause memory or other issues.

Update to clarify:

if you want to receive multidimensional array of rows there are two ways:

First: iterate over mysqli_result with fetch_assoc/fetch_array and append row to array:

$rows = array();
while ($row = mysqli_fetch_array()) {
    $rows[] = $row;
echo $rows[0]['whatever'];

Second: receive all results with one function call:

$rows = mysqli_fetch_all();
echo $rows[0]['whatever'];

That's all. No more methods are available.