Display The Last 5 Results From An SQL Table?
Moderator: General Moderators
Display The Last 5 Results From An SQL Table?
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?
$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?
- 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?
Use
to troubleshoot your queries.
In you case it will probably complain about duplicated ORDER BY clause in your query.
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?
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?
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).
List of reserved words in MySQL:
http://dev.mysql.com/doc/refman/5.0/en/ ... words.html
Code: Select all
SELECT * FROM `table` ORDER BY id DESC LIMIT 5http://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?
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!
Thanks!
-
WebbieDave
- Forum Contributor
- Posts: 213
- Joined: Sun Jul 15, 2007 7:07 am
Re: Display The Last 5 Results From An SQL Table?
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.
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?
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?
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 requestcalifdon wrote:Two postings here ask about retrieving the "last" one or several rows of a database table
Agreed.califdon wrote:you should always select records based on the data that is contained in the records
Re: Display The Last 5 Results From An SQL Table?
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.