note 51450 deleted from control-structures.while by nlopess

From: Date: Sat, 30 Apr 2005 00:49:38 +0000
Subject: note 51450 deleted from control-structures.while by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-88778@lists.php.net to get a copy of this message
Note Submitter: me at julez-edward dot be ---- When using while() to roll over a MySQL query, you can waste pretty much time with a second while going over that query. As a matter of fact, while() kind of destroys the variable that contained the query-result after being rolled over. So you need put the query again into a variable. Example: <?php $all = @mysql_db_query($db,"SELECT * FROM person"); $maxscore = 0; while ($item = @mysql_fetch_object($all)) { //get the maximum score $tempqry = @mysql_db_query($db,"SELECT * FROM scores WHERE x_person = $item->id"); $cnttempqry = @mysql_num_rows($tempqry); if ($cnttempqry > $maxscore) $maxscore = $cnttempqry; } while ($item2 = @mysql_fetch_object($all)) { echo "Second while not parsed."; //do something with the individual scores in function of the $maxscore here. } /*Hereafter you might want to use this maximum score to set this as you new reference point, and base all the other scores on that one. Also to calculate averages, this system of the double while can be useful. In fact there are people who like to do all this things with special MySQL functions. Anyway, for the second while you wont get a glimpse of what's going on, just because the while condition isn't fulfilled, right. Only solution: reload the $all query, and the while will do its job.*/ $all = @mysql_db_query($db,"SELECT * FROM person"); while ($item2 = @mysql_fetch_object($all)) { echo "Second while succesfully parsed."; //do something with the individual scores in function of the $maxscore here. } ?>

« previous php.notes (#88778) next »