Diana Hudson
Diana Hudson

Reputation: 3

Displaying MYSQL row number or position with pagination start and limit

i need a query that can display row number sequence when we have pagination. I have a problem the row number always the same when we click at next page or previous page. Please help! see hyperlink below.

$result=mysql_query("select @rownum:=@rownum+1 rownumber, t.* from tablekad t, (SELECT @rownum:=0) r order by tarikh asc LIMIT $start, $limit");

Page 1 <-- here the problem

Page 2 <---- here the link for page 2 the problem

Can with a single query we can display correctly the row number when we click the next page or previous ? Any solution for this? Please help...

Upvotes: 0

Views: 1693

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269873

You can do what you want with a subquery:

select t.*
from (select @rownum:=@rownum+1 rownumber, t.*
      from tablekad t cross join
           (SELECT @rownum:=0) r
      order by tarikh asc
     ) t
LIMIT $start, $limit;

Technically, you should probably repeat the order by on the outer query. In practice, this is probably not necessary.

Upvotes: 1

Related Questions