fetch_assoc() fails because the query returned false or true
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean givenShort answer
A fetch function needs a result set, but your query call handed it a boolean. mysqli_query() returns false when the query fails, and true for statements that make no result set, such as INSERT or UPDATE. Read the real error first, then fix the SQL or stop fetching from a statement that returns no rows.
Fix it fast
1Find out which cause you have
Print the error that MySQL reported for the query before it reaches the fetch call.
$result = $db->query($sql); var_dump($result === false ? $db->error : 'query ok');2Match what you see, then jump to its fix
- 1
The first step prints an error message, for example about a table or column nameThe query failed and returned falseFix - 2
The first step prints 'query ok' but the value is true, and the SQL is INSERT, UPDATE, DELETE or similarThe statement does not produce a result setFix - 3
On PHP 8.1 or newer you see an uncaught mysqli_sql_exception, and an if ($result === false) check never runsPHP 8.1 throws an exception instead of returning falseFix
- 1
What does the error look like?
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean givenmysqli_sql_exception
Cause 1 of 3
The query failed and returned false
1Check
mysqli_query() returns false on failure, so the fetch call receives false. The $db->error text says why.
php -r '$d=new mysqli("localhost","user","password","database"); mysqli_report(MYSQLI_REPORT_OFF); var_dump($d->query("SELECT * FROM missing_table")); echo $d->error, "\n";'2Fix
Correct the SQL (table, column or quoting) until the error is gone, and test the result before fetching.
$result = $db->query($sql);
if ($result === false) {
error_log('SQL failed: ' . $db->error);
exit('Query failed');
}
while ($row = $result->fetch_assoc()) { /* ... */ }3Verify
Run the page again; the query should now return rows.
php script.phpConfirmed by php.net
Cause 2 of 3
The statement does not produce a result set
1Check
Only SELECT, SHOW, DESCRIBE and EXPLAIN return a mysqli_result. Other successful statements return true, and true cannot be fetched from.
grep -n "fetch_" script.php2Fix
Do not fetch after a write. Read $db->affected_rows or $db->insert_id instead.
$db->query("UPDATE users SET active = 1 WHERE id = 5");
echo $db->affected_rows;3Verify
The script runs without a fetch warning.
php script.phpConfirmed by php.net
Cause 3 of 3
PHP 8.1 throws an exception instead of returning false
1Check
Since PHP 8.1 the default mysqli error mode is exceptions, so a failed query throws before your false check.
php -v2Fix
Catch the exception where you run queries. To get the old silent behaviour back, call mysqli_report(MYSQLI_REPORT_OFF), but catching is the better habit.
try {
$result = $db->query($sql);
} catch (mysqli_sql_exception $e) {
error_log($e->getMessage());
exit('Query failed');
}3Verify
Break the SQL on purpose in a test; the message should be logged instead of a fatal error.
php script.phpConfirmed by php.net
How do I stop it coming back?
- Treat every query call as something that can fail, and handle both outcomes.
- Log the database error text, not just 'query failed'.
- Use prepared statements so quoting mistakes do not break queries.
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
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 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
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