-3
+----+
| 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.

Hashmi
  • 211
  • 4
  • 14

2 Answers2

4

Did you mean

SELECT * FROM table WHERE id IN (2,3,4,5)

SELECT * FROM table WHERE id >= $id ORDER BY id ASC LIMIT 4

In the second query $id is the input from the user. Also instead of LIMIT 4, you can have the 4 taken as user input

Aditya Naidu
  • 1,362
  • 9
  • 12
  • Exactly, but I want to do it with 'Limit' because the starting id in my case will be dynamic stored in a variable, lets say $curID? – Hashmi Jun 02 '12 at 02:48
  • Lets say if there are 100 ids in DB and viewer choose a number 96, how would I show 4 records from 96? – Hashmi Jun 02 '12 at 02:50
  • Are you talking about 4 Columns contained on row 96? Then you're wanting to `SELECT col1, col2, col3, col4` If that's not the solution to your problem, you're going to need to provide alot more information and pretty much an entire breakdown of your table schema. – mawburn Jun 02 '12 at 02:51
  • Yes, I need to as I am a beginner, basically it is for an assignment for school administration that how they get students list from a specific roll number. – Hashmi Jun 02 '12 at 02:56
2

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?

Community
  • 1
  • 1
Jared
  • 301
  • 1
  • 13