how do i find the next and last id number

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
runelore
Forum Commoner
Posts: 25
Joined: Thu Aug 22, 2002 10:14 am

how do i find the next and last id number

Post by runelore »

if i have id number 13 from database and i want to find the id number of the item in teh database before this id (they are not all incremented) how do i do it... also how do i find the id number of the next item on the database.? thanks for any help..
DaiWelsh
Forum Commoner
Posts: 36
Joined: Wed Jan 08, 2003 9:39 am
Location: Derbyshire, UK

Post by DaiWelsh »

SELECT id FROM tablename WHERE id < {13 or your id variable} ORDER BY id DESC LIMIT 0,1

for last id

SELECT id FROM tablename WHERE id > {13 or your id variable} ORDER BY id ASC LIMIT 0,1

for next id.

HTH,

Dai
runelore
Forum Commoner
Posts: 25
Joined: Thu Aug 22, 2002 10:14 am

Post by runelore »

thank you very much.... that works a treat....
Post Reply