runnerjp Posted April 29, 2010 Share Posted April 29, 2010 Hey Guys, I want to get the last two posts in my db... so if my db looks like this 1 2 3 4 5 i want it to show 4 5 how can i do this?? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted April 29, 2010 Share Posted April 29, 2010 Please post in the correct forum. That's not PHP-related. If you have an index (prefer a primary key), then you can do this - SELECT * FROM table ORDER BY id DESC LIMIT 2; Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 29, 2010 Share Posted April 29, 2010 [Moved to MySQL forum] Quote Link to comment Share on other sites More sharing options...
runnerjp Posted April 29, 2010 Author Share Posted April 29, 2010 @Ken2k7 - this will then display 5 4 but i want it to be 4 5 Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted April 29, 2010 Share Posted April 29, 2010 Then SELECT from the SQL I posted and ORDER the result ASC. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 29, 2010 Share Posted April 29, 2010 Then SELECT from the SQL I posted and ORDER the result ASC. That would retrieve the first two results. The query Ken2K7 posted first gets you the results you asked for. Are you saying you can't figure out how to display them in the order you want? $query = "SELECT * FROM table ORDER BY id DESC LIMIT 2"; $result = mysql_query($query); $lastTwo = array(); while($record = mysql_fetch_assoc($result)) { $lastTwo[] = $record; } $lastTwo =array_reverse($lastTwo); foreach($lastTwo as $record) { echo $record['fieldName1']; echo $record['fieldName2']; //etc } Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted April 29, 2010 Share Posted April 29, 2010 Then SELECT from the SQL I posted and ORDER the result ASC. That would retrieve the first two results. I think you misread. SELECT * FROM (SELECT * FROM table ORDER BY id DESC LIMIT 2) t ORDER BY id ASC; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.