Understanding the "Fatal Error: Call to Undefined Method mysqli_stmt::fetch_array()" Issue
When working with MySQL databases using PHP's mysqli extension, developers may encounter the "Fatal error: Call to undefined method mysqli_stmt::fetch_array()" error. This error occurs when attempting to retrieve data from a prepared statement using the fetch_array() method, which is unavailable for prepared statements.
Remedying the Issue
To resolve this issue, it's necessary to use the correct method for retrieving data from prepared statements in MySQLi. Instead of fetch_array(), you should utilize either fetch() for retrieving a single record or fetch_all() for fetching multiple records. Here's the corrected code:
<code class="php">$search = "player"; ($sql = $db->prepare('select job from jobs where job like ?')); $sql->bind_param('s', $search); $sql->execute(); $sql->bind_result($search); $data = array(); while ($sql->fetch(MYSQLI_ASSOC)) { $data[] = array( 'label' => $row['job'] ); echo json_encode($data); } $sql->close(); $db->close();</code>
By using the correct method, mysqli_stmt::fetch(), the error can be eliminated, and data can be successfully retrieved from the prepared statement in MySQLi.
The above is the detailed content of Why Am I Getting \'Fatal Error: Call to Undefined Method mysqli_stmt::fetch_array()\'?. For more information, please follow other related articles on the PHP Chinese website!