Home >Database >Mysql Tutorial >How Can I Access mysqli_fetch_array() Results Multiple Times Without Errors?
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!