Madproject

Always know where your towel is.

PHP: Using mysqli_fetch_assoc more then once

In some of my code I need to use mysqli_fetch_assoc on the same result set more then once.

However the second time I fetch the associative array it returns 0 rows. Turns out this is because each time you use mysqli_fetch_assoc it moves the result pointer. You therefore have to reset the pointer.

Here’s an example:
This code will loop through the entire result set and the result pointer will be left pointing at the end

while($row = mysqli_fetch_assoc($result)) $this->grand_total += $row['total'];

To reset the pointer:

mysqli_data_seek($result,0); //Reset the result pointer

Leave a Reply

Your email address will not be published. Required fields are marked *