Home >Database >Mysql Tutorial >How Can I Avoid Empty Arrays When Iterating Multiple Times Through `mysqli_fetch_array()` Results?
Duplicating mysqli_fetch_array() Calls
When working with database results, it's common to require multiple iterations through the data. However, the mysqli_fetch_array() function consumes the result set, making subsequent calls return empty arrays.
Solution: Perform Data Manipulation Before Output
To avoid this issue, separate data manipulation from output by following these steps:
1. Select Data First:
2. Use Data Repeatedly:
Example Code:
// Select data and store it in an array $db_res = mysqli_query( $db_link, $sql ); $data = array(); while ($row = mysqli_fetch_assoc($db_res)) { $data[] = $row; } // Output top row foreach ($data as $row) {
The above is the detailed content of How Can I Avoid Empty Arrays When Iterating Multiple Times Through `mysqli_fetch_array()` Results?. For more information, please follow other related articles on the PHP Chinese website!