Home  >  Article  >  Database  >  Why Does My PHP Code Throw \"Fatal error: Call to undefined method mysqli_stmt::fetch_array()\"?

Why Does My PHP Code Throw \"Fatal error: Call to undefined method mysqli_stmt::fetch_array()\"?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-30 21:04:02759browse

Why Does My PHP Code Throw

Error Resolution: undefined method mysqli_stmt::fetch_array()

The error "Fatal error: Call to undefined method mysqli_stmt::fetch_array()" indicates an incorrect method call within the provided PHP code. Specifically, the method fetch_array() is not applicable to the mysqli_stmt object.`

Explanation

When using prepared statements with MySQLi, you must use the appropriate method for fetching data. For retrieving a single row of data, use mysqli_stmt::fetch(). To retrieve multiple rows, use mysqli_result::fetch_all().

Corrected Code

<code class="php">$data = array();

while ($row = $sql->fetch()) {
    $data[] = array(
        'label' => $row['job']
    );
}</code>

By replacing fetch_array() with fetch(), the code will correctly fetch the records and store them in the $data array.

It's recommended to consult the MySQLi documentation (specifically mysqli_stmt::fetch() or mysqli_result::fetch_all()) for more information on data retrieval methods with prepared statements.

The above is the detailed content of Why Does My PHP Code Throw \"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