Current Location: Home> Latest Articles> Why does calling fetch_assoc() with mysqli_result return null? Problem analysis and resolution

Why does calling fetch_assoc() with mysqli_result return null? Problem analysis and resolution

M66 2025-05-28

When using mysqli extension to connect to the database in PHP, the fetch_assoc() method of the mysqli_result object is used to obtain the next row of records from the query result set. This method returns an associative array containing the column name as the key name and the corresponding data as the value. If there are no more records to return, fetch_assoc() returns null . However, many developers will encounter a problem during use: even if the query seems to have no errors, fetch_assoc() still returns null . This article will explore the causes of this problem and provide solutions.

1. Problem analysis

When fetch_assoc() is called with mysqli_result to return null , there are several possible reasons:

1.1. The query has no results

The most common case is that the query executed does not return any data. For example, if you execute a SELECT query but there is no record in the database that meets the criteria, fetch_assoc() returns null . This is not an error, but a normal behavior, indicating that there is no more data to return.

 $query = "SELECT * FROM users WHERE id = 10";  // Assumptions id = 10 Does not exist
$result = $mysqli->query($query);

$row = $result->fetch_assoc();
if ($row === null) {
    echo "No record found!";
}

1.2. There is an error in the query statement

If there is an error in the SQL query statement itself, mysqli_query() will return false , and mysqli_result will not be created. If you do not check whether the query result is successfully executed, you may mistakenly think that the query execution is successful, but in fact, it does not return a valid result.

 $query = "SELEC * FROM users WHERE id = 10";  // SQL Syntax error
$result = $mysqli->query($query);

if ($result === false) {
    echo "Query failed: " . $mysqli->error;
}

1.3. All results have been extracted

When fetch_assoc() is called, if the data in the result set has been completely extracted, subsequent fetch_assoc() calls will return null , which means that the query has no more data to return.

 $query = "SELECT * FROM users";
$result = $mysqli->query($query);

while ($row = $result->fetch_assoc()) {
    echo $row['name'] . "<br>";
}

// Called again here fetch_assoc() Will return null
$row = $result->fetch_assoc();
if ($row === null) {
    echo "No more data!";
}

1.4. Other errors occurred during database connection or query

If a database connection error or other problem occurs during the query, the mysqli_result object may not return the data correctly. At this time, you can debug and fix the problem by checking the error message of mysqli .

 $mysqli = new mysqli('localhost', 'root', 'password', 'database');
if ($mysqli->connect_error) {
    die('Connection failed: ' . $mysqli->connect_error);
}

$query = "SELECT * FROM users";
$result = $mysqli->query($query);

if ($result === false) {
    echo "Query failed: " . $mysqli->error;
}

2. Solution

2.1. Check the query results

First, make sure that the query executed does return valid results. You can avoid unnecessary errors by checking whether the query is successful:

 $query = "SELECT * FROM users WHERE id = 10";
$result = $mysqli->query($query);

if ($result === false) {
    echo "Query failed: " . $mysqli->error;
} else {
    $row = $result->fetch_assoc();
    if ($row) {
        echo "Find the record: " . $row['name'];
    } else {
        echo "No record found!";
    }
}

2.2. Check the correctness of SQL statements

Make sure that the SQL statement is free of errors. Detailed error information can be obtained by using the mysqli_error() or mysqli_errno() functions.

 $query = "SELECT * FROM users WHERE id = 10";
$result = $mysqli->query($query);

if ($result === false) {
    echo "Query failed: " . $mysqli->error;
}

2.3. Ensure the connection is normal

Confirm that there is no problem with the database connection. Connection errors are often one of the common causes of query failure.

 $mysqli = new mysqli('localhost', 'root', 'password', 'database');
if ($mysqli->connect_error) {
    die('Connection failed: ' . $mysqli->connect_error);
}

2.4. Debugging

During the debugging process, you can use functions such as var_dump() or print_r() to output the content of the result set to help you check the returned results.

 $query = "SELECT * FROM users";
$result = $mysqli->query($query);

var_dump($result);  // Check the query result object

3. Summary

The fetch_assoc() method of mysqli_result returns null not necessarily wrong, it is a normal state in the query result set, indicating that there is no more data available for extraction. This behavior can be avoided by carefully checking whether the query returns results, whether the SQL is executed correctly, and whether the database connection is normal. I hope that the analysis and solutions in this article can help you better understand and solve this problem.