Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cant display a query twice

Status
Not open for further replies.

Aeros

Programmer
Joined
Oct 7, 2002
Messages
166
Location
US

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]

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top