Home >Backend Development >PHP Tutorial >Why Does 'Call to a member function fetch_assoc() on a non-object' Occur in PHP MySQLi and How Can It Be Fixed?
Fatal Error: Call to fetch_assoc() on a Non-Object
While attempting to retrieve a page of image information, a PHP developer encountered a fatal error: "Call to a member function fetch_assoc() on a non-object." This error indicates that the $result variable, which should hold a MySQLi resultset, is not an object.
To resolve this issue, the developer needs to verify the query executed by the database->query() method. If the query execution fails, MySQLi->query() returns false, leading to the error. The code should be modified to handle potential query errors as shown below:
By incorporating this error handling, any query errors will be captured and thrown as exceptions, allowing for appropriate error handling and resolution.
The above is the detailed content of Why Does 'Call to a member function fetch_assoc() on a non-object' Occur in PHP MySQLi and How Can It Be Fixed?. For more information, please follow other related articles on the PHP Chinese website!