Resetting the pointer in an array used for a while() loop
| From: | Dave G | Date: | Mon, 03 May 2004 10:09:25 +0000 |
| Subject: | Resetting the pointer in an array used for a while() loop | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-185180@lists.php.net to get a copy of this message | ||
PHP Listers,
In my code, I call for some data from MySQL and use the returned
results to drive a while() loop, as in the following code:
<?php
$queryA = "SELECT row1, row2 FROM table WHERE row1=" . $variable;
$resultA = mysql_query($queryA);
while($array = mysql_fetch_array($resultA))
{
//Do stuff
}
?>
The problem is that later on in the code, I want to do another
while() loop, using the same results from $QueryA. But having already
been used in a while loop already, the results of
mysql_fetch_array($ResultA) seem to be at the end of the list.
So I want to set the pointer back to the start, but using
reset() doesn't seem to have an effect. I assume this is because the
while loop is not cycling through the contents of $array, but through
the contents of $resultA. However, $resultA is not actually an array,
but the source data which gets made into an array by
mysql_fetch_array(). So, saying reset($resultA) returns an error.
How can I use the results of mysql_fetch_array($resultA) for a second
while() loop?
I hope my question is clear. Any advice would be much appreciated. Thank
you.
--
Yoroshiku!
Dave G
ml@autotelic.com