Jump to content

Limit last 5


jeff5656

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/187712-limit-last-5/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/187712-limit-last-5/#findComment-991004
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.