Search code examples
phpmysqlwebphpmyadmin

MySQL: How to show records after a specific ID


+----+
| id |
+----+
| 1  |
+----+
| 2  |
+----+
| 3  |
+----+
| 4  |
+----+
+----+
| 5  |
+----+

How do I show records from a specific ID so the list would show 2,3,4,5 or 3,4,5?

I figured to do with two queries in UNION but end up showing 2,1,3,4,5 or 2,5,4,3,1.


Solution

  • Maybe

    $sql = "SELECT * FROM table WHERE id >= $curID ORDER BY id ASC LIMIT 4";
    

    If you want to specify how many entries to find, you can use:

    $sql = "SELECT * FROM table WHERE id >= $curID ORDER BY id ASC LIMIT $number";
    

    Be sure you sanitize the inputs before plugging them into the query. See this post for more info: What's the best method for sanitizing user input with PHP?