Home > Database > Mysql Tutorial > Why Am I Getting \'Fatal Error: Call to Undefined Method mysqli_stmt::fetch_array()\'?

Why Am I Getting \'Fatal Error: Call to Undefined Method mysqli_stmt::fetch_array()\'?

Linda Hamilton
Release: 2024-11-02 10:38:30
Original
371 people have browsed it

Why Am I Getting

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

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!

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