how to get immediate next item in database?

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
Yourchild
Forum Newbie
Posts: 16
Joined: Mon Jul 30, 2007 3:04 pm

how to get immediate next item in database?

Post by Yourchild »

I have a table that looks something like this:
___________
|ID | Message|
------------------
1 a
2 b
3 c
4 d



Say I am currently accessing the 4th row (ID=4, Message=d), so my
currentID=4, currentMessage='d'

I want to access the NEXT row where ID < currentID, which is the row with
ID=3, Message=c

my query statement to access this next row looks like this:
$query = "select * from messagesTable where ID < currentID limit 1";

the problem with this query is that mysql returns the first row, which is:
currentID=1, Message=a

How do I structure the $query statement to access the next row where ID=3, Message=c?

(please excuse the appearance of the table...thats actually 2 columns squashed together..I couldlnt space them out)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Code: Select all

select * from `messagesTable` where `ID` < currentID ORDER BY `ID` DESC limit 1
Yourchild
Forum Newbie
Posts: 16
Joined: Mon Jul 30, 2007 3:04 pm

Post by Yourchild »

works like a charm! thanks a lot!!! :D
Post Reply