rondog Posted May 2, 2008 Share Posted May 2, 2008 Hi, I was wondering how I could select the last 5 rows in a table? I know I could do mysql_num_rows and then subtract 1 then 2 then 3 etc to get it, but I was wondering if their is a better way to do it. Thanks! Link to comment https://forums.phpfreaks.com/topic/103892-solved-last-rows-in-a-table/ Share on other sites More sharing options...
Rohan Shenoy Posted May 2, 2008 Share Posted May 2, 2008 <?php $sql="SELECT * FROM table_name ORDER BY id DESC LIMIT 0,5"; //selects the last 5 rows. ?> Link to comment https://forums.phpfreaks.com/topic/103892-solved-last-rows-in-a-table/#findComment-531834 Share on other sites More sharing options...
revraz Posted May 2, 2008 Share Posted May 2, 2008 That gives the last 5 IDs. As long as you use auto increment that should work fine. But if you do any type of manual ID numbering, that may not be what you want. Link to comment https://forums.phpfreaks.com/topic/103892-solved-last-rows-in-a-table/#findComment-531837 Share on other sites More sharing options...
rondog Posted May 2, 2008 Author Share Posted May 2, 2008 <?php $sql="SELECT * FROM table_name ORDER BY id DESC LIMIT 0,5"; //selects the last 5 rows. ?> cool that worked thanks! @rev. I do have auto incrementing fields, but you are right if their wasn't an auto increment field, it would be tough. Link to comment https://forums.phpfreaks.com/topic/103892-solved-last-rows-in-a-table/#findComment-531840 Share on other sites More sharing options...
revraz Posted May 2, 2008 Share Posted May 2, 2008 Nah, just use a unix time stamp instead if you run into that. Link to comment https://forums.phpfreaks.com/topic/103892-solved-last-rows-in-a-table/#findComment-531844 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.