Current Location: Home> Latest Articles> Type error: Try to call a method on a non-mysqli_result object

Type error: Try to call a method on a non-mysqli_result object

M66 2025-05-28

When using PHP for database operations, it is one of the common phenomena to encounter errors. Among them, the error "calling a method on an object that is not mysqli_result" is usually related to database query results processing. Let’s analyze the cause of this error and provide ideas on how to solve it.

1. Error background

In PHP, when using the mysqli extension for database operations, we usually execute queries and expect to get a result set from the database, which is a mysqli_result object. This object can use a series of methods to access the results of a query.

For example, execute the following SQL query:

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

The $result variable should be a mysqli_result object. If the query is successful, you can continue to use it to get the query data:

 $row = $result->fetch_assoc();  // Get a line of data

However, in some cases, $result may not be of type mysqli_result , but an other object or variable. At this time, an error like "Calling a method on an object that is not mysqli_result" will be reported.

2. Cause of error

The causes of this error are usually the following:

  • Query failed : If the query() method fails to execute, it returns false instead of the mysqli_result object. At this time, if you continue to call fetch_assoc() or other methods on $result , a type error will occur.

    For example:

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

    This code ensures that the method mysqli_result is not continued to be called when the query fails, thus avoiding type errors.

  • Connection error : If the database connection fails, the $mysqli object itself does not work properly. Ensure the database connection is successful:

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

    If the database connection is not successful, subsequent queries will not be executed, so $result will not be of type mysqli_result .

  • Call error : Sometimes due to logical problems, the program may call methods such as fetch_assoc() in an incorrect place, resulting in $result not a query result object.

    When solving this problem, you can add debug code to check the type of $result to confirm that it is a mysqli_result object:

     var_dump($result);
    

3. Solution

To avoid this type error, make sure to check if $result is a valid mysqli_result object after the query operation. Here is an improved code example:

 <?php
$mysqli = new mysqli("localhost", "user", "password", "database");

// Check if the connection is successful
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

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

// Check whether the query is successful
if ($result === false) {
    die("Query failed: " . $mysqli->error);
}

// Process query results
while ($row = $result->fetch_assoc()) {
    echo "userID: " . $row['id'] . " user名: " . $row['name'] . "<br>";
}
?>

By checking the return value of the query() method, make sure it returns a valid mysqli_result object, so that type errors can be avoided.

4. Summary

When using mysqli extension in PHP, errors often occur with query failures or incorrect object type calls. Ensuring that the result set is valid after each query and avoiding calling methods on the wrong object is the key to solving this type of error.