Maaz Zindani

Loading the index…

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

fetch_assoc() fails because the query returned false or true

ErrorWarning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given

Short 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.

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

Fix it fast

  1. 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');
  2. 2Match what you see, then jump to its fix

    1. 1The first step prints an error message, for example about a table or column nameThe query failed and returned falseFix
    2. 2The 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. 3On 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

What does the error look like?

  • Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given
  • mysqli_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.php

Confirmed by php.net

Did this fix it?

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.php

2Fix

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.php

Confirmed by php.net

Did this fix it?

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 -v

2Fix

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.php

Confirmed by php.net

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

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