jeff5656 Posted January 8, 2010 Share Posted January 8, 2010 Ok this may be a simple question but I have this query: $consultsq1 = "SELECT * FROM icu INNER JOIN bundle ON icu.id_incr = bundle.pt_id AND icu.id_incr = '" . $_GET['id'] . "' order by bundle.bundle_date LIMIT 5"; I want to limit the LAST 5, but the above limits the FIRST 5. However, I don't want to change it to DESC order. I want it to be in ascending, but only display the last 5 (newest) records. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/187712-limit-last-5/ Share on other sites More sharing options...
Lamez Posted January 8, 2010 Share Posted January 8, 2010 What the heck, why did it post that? Anywho here is the code I was working on: ------------ Okay so you want to take the last five rows, and keep the order in DESC? <?php function clean($str){ $str = @trim($str); if(get_magic_quotes_gpc()){ $str = stripslashes($str); } return mysql_real_escape_string($str); } $q = mysql_query("SELECT * FROM icu INNER JOIN bundle ON icu.id_incr = bundle.pt_id AND icu.id_incr = '" . clean($_GET['id']) . "' order by bundle.bundle_date LIMIT 5"); //Never use a $_GET variable straight into a query. Anything that can be modified from the outside, needs to be clean. $n = mysql_num_rows($q); if($n > 0){ $f = mysql_fetch_array($q); for($i=0; $i<=$n; $i++){ if($i >= ($n-5)){ echo $f['value']; } } }else{ echo "No Data"; } ?> I have not tested this, it should work, I think. Quote Link to comment https://forums.phpfreaks.com/topic/187712-limit-last-5/#findComment-991004 Share on other sites More sharing options...
cags Posted January 8, 2010 Share Posted January 8, 2010 To my knowledge this cannot be done in MySQL you will have to iterate through the resource object backwards using mysql_data_seek, or create an array with PHP, use one of the sort functions and loop through it again for output. Quote Link to comment https://forums.phpfreaks.com/topic/187712-limit-last-5/#findComment-991008 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.