mysqli_fetch_array() says the argument must be a mysqli_result, but got something else
mysqli_fetch_array(): Argument #1 must be of type mysqli_resultShort answer
mysqli_fetch_array() only accepts a mysqli_result object. You passed it something else, almost always false from a query that failed, or true from a statement that is not a SELECT. Look at what mysqli_query() returned before you fetch, and turn on mysqli exceptions so the real SQL error shows up first.
Fix it fast
1Find out which cause you have
Dump what the query call returned, and the error text mysqli kept. If the dump shows bool(false), the query failed and the error line says why.
<?php $result = mysqli_query($link, $sql); var_dump($result); echo mysqli_error($link);2Match what you see, then jump to its fix
- 1
var_dump($result) prints bool(false) and mysqli_error() has a message such as an unknown column or tableThe query failed and returned falseFix - 2
var_dump($result) prints bool(true) right after an INSERT, UPDATE, DELETE or CREATEThe statement is not a SELECT, so mysqli_query() returned trueFix - 3
var_dump() of the variable shows a mysqli_stmt object, not a mysqli_resultA prepared statement or another object was passed instead of the resultFix
- 1
What does the error look like?
mysqli_fetch_array(): Argument #1 must be of type mysqli_resultmysqli_sql_exception (PHP 8.1 and later, when the query itself failed)
Cause 1 of 3
The query failed and returned false
1Check
The manual says mysqli_query() returns false on failure. The fetch call then receives false instead of a result object. Print the SQL string and run it by hand in the mysql client.
<?php
echo $sql, PHP_EOL;
echo mysqli_error($link), PHP_EOL;2Fix
Fix the SQL the error points at. Then stop trusting the return value blindly: either test for false, or switch on strict reporting so a failed query throws mysqli_sql_exception at the line that caused it. From PHP 8.1 the default mode already throws, so there you see the exception first.
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
$result = mysqli_query($link, $sql);
while ($row = mysqli_fetch_array($result)) {
// use $row
}
} catch (mysqli_sql_exception $e) {
error_log($e->getMessage());
}3Verify
Run the page again. A broken query now logs its own message instead of a type error, and a good query returns rows.
php -d display_errors=1 page.phpConfirmed by php.net
Cause 2 of 3
The statement is not a SELECT, so mysqli_query() returned true
1Check
Only queries that produce a result set (SELECT, SHOW, DESCRIBE, EXPLAIN) return a mysqli_result. Any other successful statement returns true. Check which statement is stored in $sql at that point, especially if one variable is reused for a write and then a read.
<?php
var_dump($result instanceof mysqli_result);2Fix
Fetch only from a SELECT. After a write, read mysqli_affected_rows() instead, and run a separate SELECT when you need rows back.
<?php
$ok = mysqli_query($link, "UPDATE users SET active = 1 WHERE id = 7");
echo mysqli_affected_rows($link);
$result = mysqli_query($link, "SELECT id, email FROM users WHERE id = 7");
$row = mysqli_fetch_array($result);3Verify
The fetch line now receives an object, so the type error is gone.
php page.phpConfirmed by php.net
Cause 3 of 3
A prepared statement or another object was passed instead of the result
1Check
The manual lists what $result may be: the value from mysqli_query(), mysqli_store_result(), mysqli_use_result() or mysqli_stmt_get_result(). A statement object from mysqli_prepare() is not one of them.
grep -n "mysqli_prepare\|mysqli_fetch_array" page.php2Fix
Call mysqli_stmt_get_result() on the executed statement and pass that to the fetch function.
<?php
$stmt = mysqli_prepare($link, "SELECT id, email FROM users WHERE id = ?");
mysqli_stmt_bind_param($stmt, "i", $id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_array($result);3Verify
Run the page; the fetch returns a row array, null when there are no more rows, or false on failure.
php page.phpConfirmed by php.net
How do I stop it coming back?
- Call mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT) once, right after you load the database file, on every PHP version.
- Never pass the return value of mysqli_query() to a fetch function without knowing the statement was a SELECT.
- Use one variable per query so a write cannot overwrite the SELECT you meant to read.
- Log the exception message on the server and show visitors a plain error page.
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
Fatal error when you read a mysqli query result like an array
Fatal error: Cannot use object of type mysqli_result as array
mysqli::query() does not return rows.
2 causes, each with a fixBack 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 fix