Home >Backend Development >PHP Tutorial >How to Fix 'Call to a member function bind_param() on a non-object' and 'mysqli_fetch_array(): Argument #1 must be of type mysqli_result' Errors in MySQLi?
MySQLi Error Propagation: Resolving mysqli_fetch_array() and Bind_param() Issues
The Problem:
Errors like "Call to a member function bind_param() on a non-object" and "mysqli_fetch_array(): Argument #1 must be of type mysqli_result" can arise when using MySQLi in certain environments. This issue typically stems from a lack of configuration for MySQL error reporting in PHP.
How to Resolve:
1. Enable MySQL Error Reporting
Start by enabling MySQL error reporting in PHP by adding the following line before establishing the MySQLi connection:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
This configuration will ensure that any MySQL errors are propagated as PHP exceptions, making them visible and actionable.
2. Utilize Prepared Statements
Replace any explicit PHP variables within the SQL query with question marks and execute the query using a prepared statement. This approach helps prevent syntax errors and injection vulnerabilities.
// Example using MySQLi prepared statements $mysqli = new mysqli(...) or throw new Exception('Could not connect to DB'); $query = 'SELECT id, description FROM tbl_page_answer_category WHERE cur_own_id = ?'; $stmt = $mysqli->prepare($query); $stmt->bind_param('i', $cur_id); $stmt->execute(); $stmt->bind_result($uid, $desc);
Additional Tips
The above is the detailed content of How to Fix 'Call to a member function bind_param() on a non-object' and 'mysqli_fetch_array(): Argument #1 must be of type mysqli_result' Errors in MySQLi?. For more information, please follow other related articles on the PHP Chinese website!