Home > Backend Development > PHP Tutorial > Why is my MySQLi query returning a 'fetch_assoc() on a non-object' error?

Why is my MySQLi query returning a 'fetch_assoc() on a non-object' error?

DDD
Release: 2024-12-06 17:40:14
Original
1009 people have browsed it

Why is my MySQLi query returning a

MySQLi Query Returning "fetch_assoc() on a non-object" Error

While attempting to retrieve a page of image information using a custom function, a user encountered the following error:

Fatal error: Call to a member function fetch_assoc() on a non-object
Copy after login

The query in question selects a page of images based on their popularity using the fetch_assoc() function to retrieve each row from the result set as an associative array. However, the $result variable, which should hold the result set, was returning a non-object value.

To resolve this issue, the user was advised to check for errors in their query using MySQli->query(). If an error occurred, the error message should be displayed as an exception. Here's the suggested code change:

$result = $this->database->query($query);
if (!$result) {
    throw new Exception("Database Error [{$this->database->errno}] {$this->database->error}");
}
Copy after login

By handling errors in this manner, the user can ensure that the query is executed successfully before attempting to fetch the associative array using fetch_assoc().

The above is the detailed content of Why is my MySQLi query returning a 'fetch_assoc() on a non-object' error?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template