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?

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?

Linda Hamilton
Release: 2024-12-21 15:04:14
Original
370 people have browsed it

How to Fix

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);
Copy after login

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);
Copy after login

Additional Tips

  • Ensure that the error output is enabled, either on-screen for development servers or through error logs for live servers.
  • Examine the error message carefully and identify the actual issue.
  • Trust the error message and assume that the errors it reports are genuine.
  • Implement basic debugging techniques to verify code execution and check input data integrity.

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!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template