Friday , 22 May 2015
Home » MYSQL » Find Next and previous mysql records relative to current page
Find Next and previous mysql records relative to current page

Find Next and previous mysql records relative to current page

Hello Friends,

I have photo details page and i need to put next photo and previous photo link. To achieve this, i need to get previous record and next records relative to index or current photo record from mysql photo table. Generally we assume that it can be achieve by add one in current record’s auto increment id and minus one in current record’s auto increment id. But this is not the correct way to get next and previous record as there might be chances that administrator may remove some photo and it directly affect the auto increment id and as a result user redirect to 404 not found page.

There are some other way to get this. One of those is to get all record and compare with current record. But this will increase load of your page. Here i am showing you the optimum way to achieve this.

Find Next record:
“Select min(primaryid) as next from photos where primaryid > ‘$current_id'”

Find Previous record:
“Select max(primaryid) as previous from photos where primaryid < '$current_id'"

Let me know if you find any problem.

About Rakshit Patel

I am PHP Freelancer, Rakshit Patel AKA WordPress Freelancer working as PHP Programmer India. I am here to share all my developing knowledge, quick coding techniques, quick solutions to save your time and help you guys for any issue or bug fixing. If you have any PHP, WordPress, Magento related work, please contact me. I am interested for the work.


  1. What’s up, every time i used to check website posts here in the early hours
    in the daylight, for the reason that i love to gain knowledge of more and

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>