Skip to content

mysqli::query(): Couldn''t fetch mysqli

This error message is typically encountered when an attempt to execute a MySQLi query has failed. Specifically, it means the mysqli::query() method received an invalid or non-existent mysqli object (often false or null). This almost always happens because the database connection failed or the connection variable was not properly assigned.

Common causes:

  1. Connection failure: mysqli_connect() or new mysqli() failed due to incorrect credentials, a downed server, or network issues.
  2. Uninitialized or out-of-scope variable: The connection object was never created, or it fell out of scope before query() was called.
  3. Overwritten variable: The connection variable was accidentally reassigned to a non-object value.

To troubleshoot this issue, you can try the following:

  1. Verify the connection: Always check if the connection succeeded before executing queries. Use mysqli_connect_error() or the object's connect_error property to catch failures early.
  2. Add a minimal reproducible example:
    php
    <?php
    $mysqli = new mysqli('localhost', 'username', 'password', 'database');
    
    // Check for connection errors
    if ($mysqli->connect_error) {
        die('Connection failed: ' . $mysqli->connect_error);
    }
    
    // Now it's safe to query
    $result = $mysqli->query("SELECT * FROM users");
    ?>
  3. Debug the variable: Ensure the $mysqli variable holds a valid object when query() is called. You can use var_dump($mysqli) or if (!$mysqli instanceof mysqli) to verify its state.

If you are still having trouble after trying these steps, please provide more information about your code and the context in which the error is occurring, and I would be happy to help further.

Dual-run preview — compare with live Symfony routes.