Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Fri Jun 11, 2004 1:48 am
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
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Fri Jun 11, 2004 2:03 am
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
}
Wayne
Forum Contributor
Posts: 339 Joined: Wed Jun 05, 2002 10:59 am
Post
by Wayne » Fri Jun 11, 2004 3:22 am
mysql_data_seek($row, 0);
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Fri Jun 11, 2004 2:35 pm
Thanks Wayne. This is what I was looking for. I somehow overlooked this in the manual.