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
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}"); }
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!