Home >Database >Mysql Tutorial >How Can I Access mysqli_fetch_array() Results Multiple Times Without Errors?

How Can I Access mysqli_fetch_array() Results Multiple Times Without Errors?

Linda Hamilton
Linda HamiltonOriginal
2024-12-02 18:46:11407browse

How Can I Access mysqli_fetch_array() Results Multiple Times Without Errors?

Overcoming the mysqli_fetch_array Twice Limitation

In working with database results, you may encounter a scenario where you need to access the returned data multiple times using mysqli_fetch_array(). However, this may not be straightforward as the following error will occur:

Fatal error: Cannot call mysqli_fetch_array() on closed connection

To effectively iterate over the result multiple times, follow these steps:

Separate Data Manipulation from Output

Data manipulation and output should be handled as distinct processes. First, retrieve and store the data in an array:

$db_res = mysqli_query($db_link, $sql);
$data = array();
while ($row = mysqli_fetch_assoc($db_res)) {
    $data[] = $row;
}

Access Data Multiple Times

Once the data is stored, you can access it repeatedly:

//Top row
foreach ($data as $row) {

The above is the detailed content of How Can I Access mysqli_fetch_array() Results Multiple Times Without Errors?. 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