Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

This error means the value before ->fetch_assoc() is false, not a mysqli_result. The database operation immediately before it failed. Check the query error, fix that underlying problem, and only call fetch_assoc() after confirming you have a result object.

What the error means

A typical cause is code like $mysqli->query($sql)->fetch_assoc(). MySQLi’s query() returns a mysqli_result for successful statements that produce a result set, such as SELECT, SHOW, DESCRIBE, and EXPLAIN. If the query fails, it returns false; PHP then reports a fatal error when the code tries to call fetch_assoc() on that boolean.

An empty result is different: a successful SELECT with no matching rows still returns a result object. Fetching from it yields no row; it does not mean the query returned false.

Find the database error before the fatal line

Save the query result in a variable and check it before fetching. This exposes the original MySQLi error instead of letting the later method call obscure it:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
$result = $mysqli->query($sql);

if ($result === false) {
    error_log('Database query failed: ' . $mysqli->error);
    throw new RuntimeException('The database query failed.');
}

while ($row = $result->fetch_assoc()) {
    // Process $row.
}

$mysqli->error contains the last error message from a MySQLi call that can succeed or fail. You can also use mysqli_error($mysqli). Log diagnostic details somewhere protected; show visitors a generic message rather than credentials, connection strings, sensitive query values, or raw database errors.

Check these causes in order

  1. SQL syntax: Inspect the statement for malformed syntax, missing values, or quoting mistakes.
  2. Schema names: Verify that the table and column names exist in the database selected by this connection, and check their spelling.
  3. Connection and access: Confirm that the connection succeeded, the server is reachable, and its account has permission to run the statement.
  4. Server constraints: Check whether server limits or other constraints are preventing the query from completing.

The message alone does not identify which cause applies. The error text from the failed database operation is the clue to investigate.

Choose how your code should handle query failures

Approach How failure appears Key consideration
Check the result explicitly $result === false, followed by $mysqli->error Lets the code distinguish a failed query from a successful query with zero rows; keep diagnostic output out of visitor-facing responses.
Enable strict MySQLi reporting Failed operations throw mysqli_sql_exception Surfaces the failure at the database call; handle the exception at an appropriate application boundary.

Use exceptions when they fit the application

Configure strict reporting before making the connection or query:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

try {
    $mysqli = new mysqli($host, $user, $password, $database);
    $result = $mysqli->query($sql);

    while ($row = $result->fetch_assoc()) {
        // Process $row.
    }
} catch (mysqli_sql_exception $e) {
    error_log('Database operation failed: ' . $e->getMessage());
    // Return a controlled response or recover as appropriate.
}

MYSQLI_REPORT_ERROR makes failed operations generate warnings; adding MYSQLI_REPORT_STRICT makes them throw an exception. Do not catch and silently ignore the exception: that hides the original failure rather than handling it.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use prepared statements for variable input

If the SQL includes values supplied by a user, use a parameterized prepared statement so those values are sent separately from the SQL. PHP’s MySQLi guidance also describes properly formatting and escaping input with mysqli_real_escape_string(); prepared statements are the preferred option for user-provided values. Parameterization addresses SQL input safety, but it does not replace checking for query failures.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Capture useful details when escalating

If you cannot resolve the failure, record the exact error text, PHP version, MySQL or MariaDB version, the general shape of the SQL statement, and which connection target is in use. Do not assume one numeric error code is a universal diagnosis: server behavior and MySQLi reporting can vary by environment, and PHP 8.4 changes behavior for some server wait-timeout error codes.

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.