0

Consider a database table with 100 rows and the table is updated ie the number of rows increases on a regular basis.

Suppose that when a visitor visits my website, my table consist Xrows, but I want only rows from (X-25) to (X-50) to be visible.

I need help to code a PHP such that only rows from (X-25) to (X-50) are visible

I am new to PHP ,SQL a help would aid me a lot :)

Your Common Sense
  • 152,517
  • 33
  • 193
  • 313

3 Answers3

1

You can use limit to show particular record.

The SQL query below says "return only 10 records, start on record 15":

 "SELECT * FROM Orders LIMIT 15, 10";

1) Consider, you have 100 records 0-99. Ad you want to display 20 records starting from 11th record then query will be

"SELECT * FROM Orders LIMIT 11, 20";

2) If you want to display 1st 20 records then query will be :

 "SELECT * FROM Orders LIMIT 20";

3) If you want to display latest 20 records then :

 "SELECT * FROM Orders ORDER BY orderid DESC LIMIT 20"; 
AkshayP
  • 2,053
  • 2
  • 15
  • 26
  • I want to display last 20 records in one page(I know how to do that). But I want to know how can I display the next 20 rows starting from (X-20) in descending order, where X is total number of rows and is not constant ie increases by 1 on every new row in table –  Mar 04 '16 at 07:05
  • So you want last 20 rows of the table in descending order ? – FallAndLearn Mar 04 '16 at 07:12
  • select * from orders order by id desc limit 20; – Priyanshu Mar 04 '16 at 07:12
  • I want to know how can I display the next 20 rows starting from (X-20) in descending order,in a different webpage where X is total number of rows and is not constant ie increases by 1 on every new row in table –  Mar 04 '16 at 07:23
0

In MYSQl

SELECT * FROM table LIMIT 25, 25 -- get 25 records after row 26

If you want row 25-50 in descending order you can do this by :

SELECT * FROM (SELECT * FROM ORDERS LIMIT 25, 25) ORDER BY id DESC
FallAndLearn
  • 3,745
  • 1
  • 13
  • 23
0

To get last 20 records you have inserted.. if you are using auto increment primary key orderid,you can use the query

"SELECT * FROM Orders ORDER BY orderid DESC LIMIT 20"; 
Mohammed Shafeek
  • 1,846
  • 2
  • 13
  • 24