Im having problems running a query and then I need to display it twice. The first time it comes through but the second time I run the exact same loop to display the resultset and it doesnt display. Is there some reason it will only show once?
If you do something like the following MySQL code:
mysql_connnect (....);
mysql_select_db (...);
$result = mysql_query(....);
while ($row = mysql_fetch_assoc($result))
{
...
}
//some other stuff
while ($row = mysql_fetch_assoc($result))
{
...
}
Then the second while loop will not run at all. The first one fetched all records until it received a FALSE from the function mysql_fetch_assoc(). The return of FALSE indicates you are at the end of the fetched data. The following while() loop then invokes mysql_fetch_assoc() and since you're already at the end of the return, mysql_fetch_assoc() correctly returns FALSE. The second loop doesn't run.
There are two solutions, right off the top of my head:[ul][li]use something like mysql_data_seek() to move the internal result pointer back to the beginning of the result set. Since you haven't specified which database you're using as your backend, I can't say whether that server's supporting family of functions supports an equivalent.[/li][li]Run through the data once, storing the data in an array. The display the data twice from the array. Inside the while loop which invokes mysql_fetch_assoc(), issue a command like [ignore]$data_array[] = $row;[/ignore]. You can then loop through the array using a foreach loop as many times as you need.[/li][/ul]
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.