Home > Database > Mysql Tutorial > body text

Why Does \'Fatal error: Call to undefined method mysqli_stmt::fetch_array()\' Occur When Using Prepared Statements?

Barbara Streisand
Release: 2024-11-02 01:24:02
Original
396 people have browsed it

Why Does

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

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!