Home  >  Article  >  Database  >  Why am I getting a \"Fatal error: Call to undefined method mysqli_stmt::fetch_array()\"?

Why am I getting a \"Fatal error: Call to undefined method mysqli_stmt::fetch_array()\"?

Linda Hamilton
Linda HamiltonOriginal
2024-10-31 14:36:02395browse

Why am I getting a

mysqli_stmt::fetch_array() Undefined Method Error Resolved

Facing an error "Fatal error: Call to undefined method mysqli_stmt::fetch_array()"? This error occurs when using prepared statements in PHP and attempting to retrieve data using the fetch_array() method. Here's the fix:

In your code, you use mysqli_stmt::prepare() to create a prepared statement. After executing the statement, you try to use mysqli_stmt::fetch_array() to fetch data into an associative array. However, this method is not available for prepared statements.

The correct method for fetching data from a prepared statement is mysqli_stmt::fetch(). 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()) {
    $data[] = array(
        'label' => $row['job']
    );
    echo json_encode($data);
}

$sql->close();
$db->close();</code>

The above is the detailed content of Why am I getting a \"Fatal error: Call to undefined method mysqli_stmt::fetch_array()\"?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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