find following row

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
bytte
Forum Commoner
Posts: 75
Joined: Sun Nov 23, 2003 8:20 am
Location: Belgium

find following row

Post by bytte »

I'm using a mysql database.
I display the contents of a row by passing the ID through $_GET

e.g. detail.php?ID=15

now I want a "next" and "previous" button on that page.
What's the query to accomplish this?
Just doing $ID+1 and $ID-1 wouldn't work anymore if I deleted rows in the future...

Any suggestion? I searched through the forums but didn't find a solution. (maybe i searched using the wrong keywords)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

something like

Code: Select all

select * from table_name where id < $id order by id desc limit 1
union select * from table_name where id = $id limit 1
union select * from table_name where id > $id order by id asc limit 1
[mysql_man]union[/mysql_man]
Post Reply