Princess Toledo
Princess Toledo

Reputation: 99

PHP: get the last row in database using mysql

I have three rows in a table. I'm trying to get and display the last row of my table and, instead, its always displaying the result of the middle row.

I have tried to used the "DESC" or "DESC LIMIT 1" but still its not working.

Example:

I have a 3 rows in my table of tbldata.

0:5

0:10

0:15

The query for this:

$result = mysql_query("SELECT fldBldgName, fldTimestamp,   
MIN(fldTotalDuration)fldTotalDuration   
FROM tbldata    
WHERE  fldNetname = '".$network."'   
AND fldBldgName = '".$bldg[$i]."'    
AND  fldWeek = '".$week."'   
AND fldMonth = '".$month."'   
GROUP BY fldBldgName   
ORDER BY  fldBldgName,fldTimestamp, fldTotalDuration DESC");

I already tried also the ID to order by but still its not working. I tried the timestamp, but still its not working.

The result for that query is always the "0:10", the correct output should be "0:15".

But then if I have 2 rows only:

0:5

0:10

the result is : 0:10 which is correct

And so, I also tried it to manually query it in phpmyadmin, but it still does not display the last row.

Upvotes: 1

Views: 8430

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94642

Assuming you have an id field with AUTO_INCREMENT set.

Then try this

SELECT * FROM tbl ORDER BY id DESC LIMIT 1;

Your use of MIN(fldTotalDuration) and GROUP BY was totally confusing the result you were after.

Upvotes: 3

Related Questions