Jump to content

Limit last 5


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.


Link to comment
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?

function clean($str){
$str = @trim($str);
	$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'];
echo "No Data";


I have not tested this, it should work, I think.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • 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.