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

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

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

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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
}
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

mysql_data_seek($row, 0);
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Thanks Wayne. This is what I was looking for. I somehow overlooked this in the manual.
Post Reply