Call to a member function fetch() on boolean

Call to a member function fetch() on boolean

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Call to a member function fetch() on boolean

This error, Fatal error: Call to a member function fetch() on boolean, is a classic symptom of a silent failure in your database query logic. It occurs when you attempt to iterate over a result set that wasn't returned as expected. In the specific case provided, the root cause lies within the createTable method where $this->pdo->query('SELECT * FROM article') is executed without verifying whether a valid PDOStatement object was actually returned.

When PDO::query() cannot execute a statement or fails to retrieve rows, it returns false. If you chain .fetch() directly onto this return value without checking for falsiness first, PHP throws the fatal error because the fetch() method exists on PDOStatement but not on a boolean false. This is distinct from a syntax error; it is a logical flow control issue where the code assumes success when the database layer has already signaled failure.

The Silent Failure of PDO::query()

The primary pitfall with PDO::query() is that it does not throw exceptions by default, even when the query fails. Unlike PDOStatement::execute(), which throws an exception if an error occurs unless you have disabled errors via setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT), PDO::query() simply returns false on failure.

In the provided code snippet:

$q = $this->pdo->query('SELECT * FROM article');
while($row = $q->fetch()) {
    echo $row['id'].' ';
}

If the table article does not exist, or if the query string contains a syntax error that PDO cannot parse in the current context, $q becomes false. The subsequent call to $q->fetch() attempts to invoke the method on that boolean value, triggering the crash. Even worse, if a SQLSTATE is set but no rows are returned (e.g., an empty table), query() might still return a valid statement object, but fetch() would immediately return false on the first call, causing the loop to exit cleanly. However, if the query itself fails, query() returns false.

Diagnosing the Specific Scenario

Before jumping into error handling, you must determine why the query is failing in your specific environment (C:\xampp\htdocs\repo\generator...). Common causes include:

  1. Missing Table: The database generatordatabase exists, but the article table was never created.
  2. Permissions: The root user (or the specified credentials) lacks SELECT permissions on that specific database or table.
  3. Syntax/Typo: A subtle typo in the query string, though usually, this would throw a more descriptive exception if error reporting is strict.

To resolve the fatal error immediately, you must add an explicit check for the truthiness of $q. This transforms the potential crash into a manageable warning or a specific error message that tells you exactly what went wrong.

public function createTable() {
    $q = $this->pdo->query('SELECT * FROM article');

    if ($q === false) {
        // Handle the failure gracefully
        echo "Query failed. Check if table exists and permissions are set.";
        return; 
    }

    while($row = $q->fetch()) {
        echo $row['id'].' ';
    }
    $q->closeCursor();
}

By adding if ($q === false), you intercept the failure before it reaches the fetch() call. This is a fundamental pattern in PDO development: never trust that a non-transactional read query will succeed without verification.

Error Reporting and Debugging

It is also worth noting that your current error handling relies on echo statements inside the try-catch block of the connect method. While functional for debugging, mixing logging with direct output in production code is often discouraged. Furthermore, relying solely on catch(PDOException $e) might not catch every issue if PDO returns false instead of throwing an exception, which happens with query().

To get more granular details about why the query failed, you should check the error code and message associated with the last operation on your connection:

if ($q === false) {
    $errorInfo = $this->pdo->errorInfo();
    echo "Query Error Code: " . $errorInfo[0] . "\n";
    echo "Error Message: " . $errorInfo[2];
}

This approach provides the SQLSTATE code and the native driver message, which are invaluable for distinguishing between a "table doesn't exist" error and a permission denial.

Best Practices for Iteration

Finally, consider whether fetch() is the right method for your use case. The fetch() method returns one row at a time. If you need to process all rows in an array before doing anything else, using fetchAll(PDO::FETCH_ASSOC) might be more appropriate. However, if memory efficiency is a concern with large datasets, stick with fetch().

Regardless of the method chosen, the golden rule remains: validate the result of PDO::query() before attempting to interact with the resulting statement object. This simple check prevents fatal errors and makes your application robust against missing tables or database connectivity issues that happen unexpectedly during runtime.

Our network