Page 1 of 1

[SOLVED] Point $row=mysql_fetch_array($res) to another index

Posted: Fri Jun 11, 2004 1:48 am
by anjanesh
Hello

I have this :

Code: Select all

$res=mysql_query("SELECT * FROM category");
while($row=mysql_fetch_array($res))
 {
 .....something
 }
No again I have to do the same thing for a second time (cannot be put in a function)

Code: Select all

$res=mysql_query("SELECT * FROM category");
while($row=mysql_fetch_array($res))
 {
 .....something else
 }

Is there any way to avoid the query and just point $row back to first index of $res ?

Thanks

Posted: Fri Jun 11, 2004 2:03 am
by markl999
Just store the rows in an array after first doing the query. Then you ca do whatever you want as many times as you want with that array.
Eg.

Code: Select all

$res=mysql_query("SELECT * FROM category");
while($row=mysql_fetch_array($res))
{
    $rows[] = $row;
}
foreach($rows as $row){
   //do stuff with $row
}
//do some more stuff
foreach($row as $row){
  //do more stuff here
}

Posted: Fri Jun 11, 2004 3:22 am
by Wayne
mysql_data_seek($row, 0);

Posted: Fri Jun 11, 2004 2:35 pm
by anjanesh
Thanks Wayne. This is what I was looking for. I somehow overlooked this in the manual.