This error means your MySQLi query failed and returned false, but your code then called fetch_assoc() as if it had returned a result set. The fix is to find and correct the database error reported by the query, then check for failure before fetching rows.
What the error means
fetch_assoc() is a method of a mysqli_result object. A successful SELECT, SHOW, DESCRIBE, or EXPLAIN query returns that kind of object; a failed query returns the boolean value false. Calling a result method on false causes the fatal error.
The fatal error is therefore a follow-on failure, not the original database problem. Also, it does not mean the query found no rows: a successful query with zero matching rows still returns a result object. The failure occurred while running the query or establishing the database operation.
Find the database error that happened first
Common causes include malformed SQL, a misspelled or missing table or column, invalid connection credentials, insufficient database permissions, an unavailable or closed connection, or a server limit. The specific cause depends on the SQL and the database environment, so inspect the error from the failed MySQLi operation rather than guessing from the later fetch_assoc() message.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstall#1 Best Overall
- Store the query result in a variable instead of chaining
query()directly intofetch_assoc(). - Check whether the result is exactly
false. - If it failed, read
$mysqli->erroror usemysqli_error($mysqli)to get the last MySQLi error message. - Use that message to correct the SQL, connection, schema, permission, or server issue.
- Fetch rows only after the query succeeds.
$result = $mysqli->query($sql);
if ($result === false) {
throw new RuntimeException($mysqli->error);
}
while ($row = $result->fetch_assoc()) {
// Process $row.
}
Using === false makes the failure check explicit. Once execution reaches the loop, a successful query with no matching rows simply runs zero iterations.
Choose how your application should handle query failures
| Approach | How the original failure appears | Zero-row queries | Best fit |
|---|---|---|---|
Check for false and inspect $mysqli->error |
Your code detects failure at the query call and decides how to handle it. | Handled separately: a successful result can still contain no rows. | Applications that want to handle each query failure explicitly. |
| Enable strict MySQLi reporting | With MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT, a failed operation raises a mysqli_sql_exception at the failing operation. |
A zero-row result is not a query exception. | Applications that use exception handling for database failures. |
Use explicit failure checks
The example above lets the calling code handle the error at the point where it runs the query. If you throw an exception, catch it only at a boundary where the application can recover or return a controlled response. Do not catch and silently ignore it; that hides the original problem and can lead to later failures that are harder to diagnose.
Use strict reporting for exceptions
MySQLi reporting can be configured so failed operations throw instead of allowing execution to continue with a false result:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli($host, $user, $password, $database);
$result = $mysqli->query($sql); // Throws if the operation fails.
Handle the resulting mysqli_sql_exception at an appropriate application boundary. This brings attention to the database operation that failed instead of letting the error surface later when the code tries to fetch a row.
Rank #3
Keep query diagnostics out of public error pages
During development, the database error message helps identify the failure. In production, do not display credentials, raw connection strings, or sensitive query parameters to visitors. Send diagnostic details to a protected log and show users a generic error page.
If SQL includes variable input, use a parameterized prepared statement, especially for values supplied by users. Properly formatted and escaped input is another option, but prepared statements separate values from the SQL statement.
Rank #4
Account for version and environment differences
The exact MySQLi error varies with the query, connection, server, and configuration. PHP and MySQLi reporting defaults can also differ by version and setup, so do not diagnose the issue from a numeric error code alone. When asking for help, record the PHP and MySQL or MariaDB versions, the SQL statement shape, the connection target, and the exact error text. PHP 8.4 also changes behavior for some server wait-timeout error codes, which is another reason not to hard-code a single code as the diagnosis.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Recommended Free Tools




