I have a database that contains multiple comments. Each comment has their own ID. I want to select 5 comments from the database, store the last comments ID into a variable and then select 5 more comments at a later time. When I collect 5 more comments at a later time I need them to have an ID larger than the previous comments ID that was stored in the variable. I will try to make a code so it is easier to understand.

$commentID = 0;

$query = "SELECT * FROM comments WHERE commentID > '$commentID' LIMIT 5";

I have not tried this code, I want to make sure this is the proper way of doing this before I test it and cant figure out the problem. But another question is if this does work, will it select the next 5 comments in order?
For example the commentID = 5 then will the query select comments with the ID's of 6,7,8,9,10?

Recommended Answers

All 3 Replies

LIMIT has two parameters, offset and rowcount:

LIMIT 0,5   -- first five
LIMIT 5,5   -- second five
LIMIT 10,5  -- third five

Yes I know about these but I want to have it done without using offset or Rowcount.

If you store the highest last used ID and use that in the next query (session variable), then it works like you showed. Just be sure you use ID in the order by too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.