Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you reuse a mysql result set in PHP?

I have a result set I pull from a large database:

$result = mysql_query($sql);

I loop through this recordset once to pull specific bits of data and get averages using while($row = mysql_fetch_array($result)). Later in the page, I want to loop through this same recordset again and output everything - but because I used the recordset earlier, my second loop returns nothing.

I finally hacked around this by looping through a second identical recordset ($result2 = mysql_query($sql);), but I hate to make the same SQL call twice. Any way I can loop through the same dataset multiple times?

like image 866
MarathonStudios Avatar asked Jan 09 '11 06:01

MarathonStudios


1 Answers

Use:

mysql_data_seek($result, 0);

You get this "free", since it's already buffered.

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

like image 171
Matthew Flaschen Avatar answered Oct 17 '22 00:10

Matthew Flaschen