Fixing "Fatal error: Call to undefined method mysqli_stmt::fetch_array() [duplicate]"
In your code, you're attempting to use mysqli_stmt::fetch_array() when using prepared statements. This function is not available for prepared statements.
Solution:
Instead, you should use mysqli_stmt::fetch() to retrieve a single row of data, or mysqli_result::fetch_all() to retrieve multiple rows.
Revised code:
<code class="php">$search = "player"; $sql = $db->prepare('select job from jobs where job like ?'); $sql->bind_param('s', $search); $sql->execute(); $result = $sql->get_result(); // Get the result object $data = array(); while ($row = $result->fetch_assoc()) { $data[] = array( 'label' => $row['job'] ); echo json_encode($data); } $sql->close(); $db->close();</code>
By using mysqli_stmt::fetch_array() or mysqli_result::fetch_assoc(), you can retrieve the data from the database successfully without encountering the error.
The above is the detailed content of Why Does 'Fatal error: Call to undefined method mysqli_stmt::fetch_array()' Occur When Using Prepared Statements?. For more information, please follow other related articles on the PHP Chinese website!