View Full Version : problem with order and limit

Baked Potato v3
04-20-2011, 01:35 AM
I'm kinda new to PHP & MySQL and am having a small problem with a SELECT query. Can anyone point me in the right direction on how to resolve this?

I have a table, 'discussion', with the fields 'postid', 'parent', and several others. I am trying to query 'discussion' for a range of rows with a limit of ten. Using the 'postid' of the last post, I create a link to view the next set of results after the last post.

I have no problem doing this. I have 24 rows in the table and am able to view them in sets of 10 at a time. But when I try to order the results descending, I am unable to. The first set of ten (#24-#15) display, but when I try to view the next ten, the query returns the results for rows #9-#1.

my trimmed down code:

if (!$more) {$more = 0;} # starting post
$query = "SELECT * FROM `disc` WHERE `parent` = 0 ORDER BY postid LIMIT $more, 10";
$result = $db->query($query);
$num_results = $result->num_rows;
$lastPost = 0;
for ($i=0; $i < $num_results; $i++) {
... fetch row from result and process
$lastPost = $pid;

print "<a href='?more=$lastPost' title='Next'>Next</a>";

This will display the results in ascending order and allow me to page through the results, but when I change my query to:

$query = "SELECT * FROM `disc` WHERE `parent` = 0 ORDER BY postid DESC LIMIT $more, 10";

the paging does not work.

Can anyone point out what I'm doing wrong or what I should look into to achieve this. Thanks.

Old Pedant
04-20-2011, 01:55 AM
Well, not to ask a dumb question, but have you debugged?

WHat is the value of $more on that second page???

How about simply echoing the $query on each page?

FWIW, doing DESC LIMIT start,count works fine for me.

Baked Potato v3
04-20-2011, 02:51 AM
Ok, I think I understand where I am confused and what is tripping me up.