View Full Version : Next query in Mysql

11-06-2006, 02:09 AM
Okay, I feel stupid simply because of the fact that I cannot get it to work. Im using PHP an what im trying to do its pull queries from the database. This is how I want it set up. I have posts, much like a forum post, except the page only displays one post at a time! I want to have a link to have that page pull up a whole nother post. I used to have it set up to where when you click the link it just grabbed the next ID, preset. I want it to do it automatically, because when there is a post that isnt appopriate, I delete it and then the ID's are all screwed up and not all there. Hopefully everyone can understand what Im saying. If you need further examples please just ask. I've been trying to do this for a year and I gave up after awhile.


11-06-2006, 12:18 PM
(select id from yourtable
where id = (select min(id) from yourtable
where id > dt.id
) as nextrow,
(select id from yourtable
where id = (select max(id) from yourtable
where id < dt.id
) as previousrow
from yourtable as dt

this will give you next and previous posts irrespective of whether or not there are ids missing in your table.

11-12-2006, 02:14 AM
Im still not sure how i can get that to work! I want it to show one post, then have it link to another one... hmm.. not sure how to put it into a query either! Please help explain..


11-13-2006, 03:58 PM
i've already given you the query, you may need to adapt it to your column names.

it will only show you three total items at a time, you would reference postid as your current row and reference nextrow and previous row as your row after and before that postid.

sir pannels
11-13-2006, 04:02 PM
Hmm yeah Nate that query should have solved your problems... did you need help implamenting it into a server side language?

11-23-2006, 01:09 AM
Yes, I need help using it with PHP... maybe Im just not looking at it the right way! Please help!!!

11-23-2006, 04:41 PM
I'll move the thread over to the php forum so you can get quicker help there.

you should post the part of your code where you are trying to implement this so folks can see where you are having trouble.