Page 1 of 1

Display The Last 5 Results From An SQL Table?

Posted: Fri Jul 25, 2008 1:21 pm
by Thomas-T
I'm having some trouble getting the last 5 database entries to display on the screen. Here's the code:

$return = mysql_query("SELECT * FROM table ORDER BY id DESC ORDER BY id DESC LIMIT 5");

while($row = mysql_fetch_array($return));
{
$id = $row['id'];
$name = $row['name'];
$developer = $row['developer'];
echo $developer . "-" . $name;
echo "<br /><hr />";
}

include("footer.php");
?>

I'm not getting any errors, it's just that I'm not getting any data from the database to display to the screen. Any help?

Re: Display The Last 5 Results From An SQL Table?

Posted: Fri Jul 25, 2008 2:21 pm
by EverLearning
Use

Code: Select all

mysql_query($sql) or die(mysql_error());

to troubleshoot your queries.
In you case it will probably complain about duplicated ORDER BY clause in your query.

Re: Display The Last 5 Results From An SQL Table?

Posted: Sat Jul 26, 2008 12:14 am
by coder500
Deleted up on request.

Re: Display The Last 5 Results From An SQL Table?

Posted: Sat Jul 26, 2008 1:49 am
by WebbieDave
Did you really name your table table? If so, it's a reserved word and you'll want to escape it with backticks. Also, your SQL is a little off (extra ORDER BY clause for some reason).

Code: Select all

SELECT * FROM `table` ORDER BY id DESC LIMIT 5
List of reserved words in MySQL:
http://dev.mysql.com/doc/refman/5.0/en/ ... words.html

Display The Recently Added Results From An SQL Table?

Posted: Sat Jul 26, 2008 6:28 am
by php000developer
plz tell me the codes for displaying the last data or data that was added at last or can say that recently added data. i just want to display only the last result that was added in the sql table.
Thanks!

Posted: Sun Jul 27, 2008 12:59 am
by coder500
Deleted up on request

Re: Display The Last 5 Results From An SQL Table?

Posted: Sun Jul 27, 2008 12:31 pm
by WebbieDave
Coder500, why do you answer the question on another forum, then link to it from here? Why not just cut out the middle man and post the answer here directly? You've done this numerous times on numerous posts.

Yes, I can guess why but I'd like him/her to answer for it (which he/she won't). I also want to bring his/her actions to the admins' attention and, as they undoubtedly already know, underscore the fact that this type of behavior dilutes the efficacy of this forum over time.

Re: Display The Last 5 Results From An SQL Table?

Posted: Sun Jul 27, 2008 1:07 pm
by califdon
Two postings here ask about retrieving the "last" one or several rows of a database table. This is treading on treacherous ground, because relational database theory states that row order in a database is not guaranteed. Some database engines may add rows that are physically where you would expect them to be, but others may not, and deletions or updates may affect this order, too. The point is this: you should always select records based on the data that is contained in the records, never on what you assume is the order of the records on the storage device.

Re: Display The Last 5 Results From An SQL Table?

Posted: Sun Jul 27, 2008 1:18 pm
by WebbieDave
califdon wrote:Two postings here ask about retrieving the "last" one or several rows of a database table
Well, the OP actually requested info on how to retrieve the "last 5 database entries" and he had an id field so it would seem fine in this case to assume that he needs the records with the five highest ids (and he was already approaching it as such). The other user who posed a question was just piggy-backing the OP's and demanded code while providing no information about his particular situation so we can safely ignore his request :)

califdon wrote:you should always select records based on the data that is contained in the records
Agreed.

Re: Display The Last 5 Results From An SQL Table?

Posted: Sun Jul 27, 2008 1:32 pm
by califdon
Actually, you are right. I didn't look closely enough at what he was trying to do. In any case, it was an opportunity for me to preach about the subject. A lot of people still think of a database the way they've come to think of spreadsheets, in terms of physical positioning of data, and I like to try to disabuse them of this kind of thinking.