Fatal error when you read a mysqli query result like an array
Fatal error: Uncaught Error: Cannot use object of type mysqli_result as arrayShort 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.
Fix it fast
1Find out which cause you have
Print what your variable really holds, right after the query.
$result = $mysqli->query($sql); var_dump($result);2Match what you see, then jump to its fix
What does the error look like?
Fatal error: Uncaught Error: Cannot use object of type mysqli_result as arrayFatal 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.php2Fix
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.phpConfirmed by php.net
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.php2Fix
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Back end
CodeIgniter 4: try/catch does not catch a failed insert in the model
DatabaseException
CodeIgniter throws a DatabaseException on a failed query only when DBDebug is true, and inside a transaction it does not throw unless you ask.
3 causes, each with a fixBack end
CodeIgniter 4 where() builds a different condition than you meant
A SQL syntax error that appears only when a value contains a quote character
The query builder joins every where() with AND and every orWhere() with OR, with no brackets.
3 causes, each with a fixBack end
Call to undefined function mysql_pconnect() after a PHP upgrade
Call to undefined function mysql_connect()
The old mysql_* functions belonged to the ext/mysql extension, which PHP removed in version 7.0.
2 causes, each with a fixBack end
CodeIgniter cannot connect to the MySQL database server
Unable to connect to your database server using the provided settings.
CodeIgniter only reports that its connection failed.
4 causes, each with a fixBack end
Laravel insert fails with 'Field user_id doesn't have a default value'
The insert works when the column is removed or made nullable
Error 1364 means Laravel sent an INSERT without a value for a NOT NULL column that has no default, and MySQL strict mode refuses it.
3 causes, each with a fixBack end
Laravel migration cannot drop a column that has a foreign key
Error on rename of '%s' to '%s' (errno: %d - %s)
MySQL refuses to drop a column while a foreign key constraint still uses it.
2 causes, each with a fix