Can you reuse a mysql result set in PHP?

浪尽此生 提交于 2019-12-18 16:26:31

问题


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?


回答1:


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.




回答2:


Using SQL Cursors you can get this approach



来源:https://stackoverflow.com/questions/4638014/can-you-reuse-a-mysql-result-set-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!