teslasimus
teslasimus

Reputation: 1298

mysql: select last 10 rows from DESC LIMIT

i'm working on a block which will display last 10 posts, when user clicks on "load more" button i would like to display 10 older posts.

how can i select last 10 rows if i'm already using DESC LIMIT?

mysql_query("SELECT title,id,alt_name FROM dle_post WHERE approve='1' AND date >= '$monthagodate'
AND date < '$curdate' + INTERVAL 1 day ORDER BY date DESC LIMIT $more;");

Upvotes: 2

Views: 23594

Answers (2)

Deepak
Deepak

Reputation: 6802

You should add another parameter $start and use the following query

mysql_query("SELECT title,id,alt_name FROM dle_post WHERE approve='1' AND date >= '$monthagodate' AND date < '$curdate' + INTERVAL 1 day ORDER BY date DESC LIMIT $start, $more;");

Here $start is used to tel MySQL the starting point of records to be fetched.

Upvotes: 1

Patrick Moore
Patrick Moore

Reputation: 13344

You are LIMITing using just one parameter. But you can use LIMIT x,y to specify both x (the position of the first record to return) and y (the number of records to return).

Pass a variable to the page like results.php?start=x to set a starting position.

if (!isset( $_REQUEST['start'] ) ) { $start = 0; } else { $start = (int)$_REQUEST['start']; }

mysql_query("SELECT title,id,alt_name FROM dle_post WHERE approve='1' AND date >= '$monthagodate'
AND date < '$curdate' + INTERVAL 1 day ORDER BY date DESC LIMIT $start,10;");

And then you can generate a link to the next page like so:

echo '<a href="results.php?start=' . ($_REQUEST['start']+10) . '">Next 10 results</a>';

Upvotes: 5

Related Questions