Can you reuse mysql result set in PHP?

I have a result set that I am retrieving from a large database:

$result = mysql_query($sql); 

I scroll through this recordset once to pull out certain bits of data and get average values โ€‹โ€‹using while($row = mysql_fetch_array($result)) . Later on the page, I want to skip the same recordset again and print everything, but since I used the recordset before, my second loop returns nothing.

I finally cracked this by going over the second identical set of records ( $result2 = mysql_query($sql); ), but I don't like to make the same SQL call twice. In any case, can I iterate over the same data set several times?

+6
php mysql
source share
2 answers

Using:

 mysql_data_seek($result, 0); 

You get this โ€œfreeโ€ as it is already buffered.

As a separate note, you can explicitly execute an unbuffered query with mysql_unbuffered_query .

+20
source share

Using SQL Cursors , you can get this approach

0
source share

All Articles