Harry Brand Harry Brand - 6 months ago 16
MySQL Question

PHP get next ID from MySQL

I have a database with auto incrementing ID's, each entry has a youtube videoID which is a varchar. So when the page loads it echos the first entry (ID 1), when you click the button it prints the next entry (ID 2). How would i do this?

Thanks

Answer

So when the page loads it echos the first entry (ID 1), when you click the button it prints the next entry (ID 2). How would i do this?

This sounds like ordering and paging. (Even with a "page" size of only 1.) Basically select all records greater than the "current" ID, ordered by ID, limited to 1. Something like:

SELECT *
FROM tableName
WHERE ID > ?
ORDER BY ID
LIMIT 1

So if your records are:

ID | Value
----------
1  | A
2  | B
3  | C
4  | D
5  | E

and you supply 3 as the "current ID" (the one currently being viewed on the page where the "next" link is rendered), then the query would return

4  | D

Because 4 and 5 satisfy the WHERE condition, you sort by ID so 4 is first, and you limit the results to 1 record.

Comments