PHPWarningRuntime ErrorMay 8, 2026

Runtime Error

Call to a member function fetchAll() on null in /var/www/html/database.php on line 56

What This Error Means

This error occurs when a developer tries to call a method on a null or non-object variable, which in this case is a result set from a SQL query.

Why It Happens

This error usually happens when the database query execution fails, or when the query returns no results, and the developer tries to call a method on the result set without checking if it's null.

How to Fix It

  1. 1To fix this error, you should check if the result set is not null before trying to call a method on it. You can do this by using the null coalescing operator (??) or by checking if the result set is an instance of PDOStatement.

Example Code Solution

❌ Before (problematic code)
PHP
$pdo = new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password');
$result = $pdo->query('SELECT * FROM users WHERE id = 1');

// Assuming the query returns no results, $result will be null
foreach ($result->fetchAll() as $user) {
    // Code here
}
✅ After (fixed code)
PHP
$pdo = new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password');
$result = $pdo->query('SELECT * FROM users WHERE id = 1');

if ($result !== null && $result instanceof PDOStatement) {
    foreach ($result->fetchAll() as $user) {
        // Code here
    }
} else {
    // Handle the case where the query returned no results or failed
}

Fix for Call to a member function fetchAll() on null in /var/www/html/database.php on line 56

Related PHP Errors

Related PHP Blog Articles

Have a different error? Get an instant explanation.

Explain Another Error