Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Fatal error when you read a mysqli query result like an array

ErrorFatal error: Uncaught Error: Cannot use object of type mysqli_result as array

Short answer

mysqli::query() does not return rows. For a SELECT it returns a mysqli_result object, and you must read rows from it with fetch_assoc(), fetch_all(MYSQLI_ASSOC) or a foreach loop. Writing $result['name'] on the object itself causes this fatal error.

  • Applies to PHP 8.3, PHP 8.4, MySQL 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print what your variable really holds, right after the query.

    $result = $mysqli->query($sql);
    var_dump($result);
  2. 2Match what you see, then jump to its fix

    1. 1var_dump shows object(mysqli_result) and the line that fails is $result['column'] or $result[0]The result object is indexed directlyFix
    2. 2Code does json_encode or foreach over $result['rows'] and expects a plain arrayYou want all rows as one array but did not fetch themFix

What does the error look like?

  • Fatal error: Uncaught Error: Cannot use object of type mysqli_result as array
  • Fatal error: Cannot use object of type mysqli_result as array

Cause 1 of 2

The result object is indexed directly

1Check

For SELECT, SHOW, DESCRIBE and EXPLAIN, query() returns a mysqli_result object. Any other successful query returns true and a failure returns false.

grep -n -E '\$result\[|\$query\[' index.php

2Fix

Read the rows from the object. Use fetch_assoc() in a loop, or foreach, which always starts from the first row.

$result = $mysqli->query("SELECT Name, CountryCode FROM City");
while ($row = $result->fetch_assoc()) {
    echo $row['Name'];
}

3Verify

Run the page; it prints each name instead of a fatal error.

php -l index.php && php index.php

Confirmed by php.net

Did this fix it?

Cause 2 of 2

You want all rows as one array but did not fetch them

1Check

fetch_all() builds an array of rows. The mode argument chooses the row type, and MYSQLI_ASSOC keys each row by column name. The default is MYSQLI_NUM, so column names do not work without the flag.

grep -n 'fetch_all' index.php

2Fix

Call fetch_all(MYSQLI_ASSOC) once and loop over the returned rows.

$rows = $result->fetch_all(MYSQLI_ASSOC);
foreach ($rows as $row) {
    echo $row['Name'];
}

3Verify

Dump the array and confirm each element is a row.

var_dump(count($rows), $rows[0] ?? null);

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Call mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT) so a failed query throws instead of returning false.
  • Name the variable $result for the object and $rows for fetched data so the two never get mixed.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.