Page 1 of 1

how to get immediate next item in database?

Posted: Sun Sep 16, 2007 12:09 pm
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)

Posted: Sun Sep 16, 2007 12:23 pm
by superdezign

Code: Select all

select * from `messagesTable` where `ID` < currentID ORDER BY `ID` DESC limit 1

Posted: Sun Sep 16, 2007 12:36 pm
by Yourchild
works like a charm! thanks a lot!!! :D