Moving to a specific row in MySQL result set

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
ruthsimon
Forum Newbie
Posts: 19
Joined: Mon Aug 19, 2002 5:44 pm

Moving to a specific row in MySQL result set

Post by ruthsimon »

I have a page which first displays all rows of a MySQL result set (which leaves me at the end of the set). I then want to go to a specific row in the set based on a value in the field (I can't use a row offset because I don't know which row has that value).

I could just do another query with a WHERE statement, but it seems like I shouldn't have to since I've already got the entire set.

Code: Select all

$event_query = "SELECT * FROM event"; 
$event_result = mysql_query($event_query)
   or die ("Couldn't execute event query");

//display all rows
while ($event_row = mysql_fetch_array($event_result,MYSQL_ASSOC))  {
   extract ($event_row);
   echo "\$event_id: $event_id<br>
      \$event_name: $event_name<br>";
&#125;

//go back to first record in result set.
$new_event_row = mysql_data_seek($event_result,0);
Now I want to go to the row where the field $event_id == $number (a session variable) so that the results of that row can be displayed in a form for editing. Any help with code to do this without running the query again?

Thanks, Ruth
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Just stick an if test in the while loop.
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

mysql_result ( resource result, int row [, mixed field])
?
Post Reply