Display The Last 5 Results From An SQL Table?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Thomas-T
Forum Newbie
Posts: 7
Joined: Thu Jul 24, 2008 11:32 am

Display The Last 5 Results From An SQL Table?

Post 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?
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

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

Post 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.
coder500
Forum Newbie
Posts: 20
Joined: Fri Jul 25, 2008 10:24 am
Location: Singapore

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

Post by coder500 »

Deleted up on request.
Last edited by coder500 on Mon Jul 28, 2008 10:28 am, edited 1 time in total.
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

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

Post 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
php000developer
Forum Newbie
Posts: 4
Joined: Fri Jul 25, 2008 6:24 am

Display The Recently Added Results From An SQL Table?

Post 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!
coder500
Forum Newbie
Posts: 20
Joined: Fri Jul 25, 2008 10:24 am
Location: Singapore

Post by coder500 »

Deleted up on request
Last edited by coder500 on Mon Jul 28, 2008 10:20 am, edited 1 time in total.
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

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

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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.
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

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

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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.
Post Reply