View Full Version : SQL & PHP : Retrieve the last 10 records in table
Hi everyone,
well this is my 1st post & im wondering if someone can give me the syntax to display the last 10 entries in a table
for example if there are 100 records 1 to 100 i want to select 90 - 100
any help would be appreciated
regards
Chris
add as primary key to your table : id => auto increment
then use query (something like):
$query = "SELECT * FROM table ORDER BY id DESC LIMIT 10";
TO select the other 10one (instead of the first the last) use instead of DESC ASC
so :
$query = "SELECT * FROM table ORDER BY id ASC LIMIT 10";
Don't forget to add a field called 'id' to your table. With phpmyadmin you can choose on 'extra' => auto_increment and then select is as primary key.
Good luck
Arne
EDIT : addes limit cluase after arnyinc's post. Forgot it
arnyinc
03-12-2006, 07:14 PM
http://dev.mysql.com/doc/refman/5.0/en/select.html
Look at the section discussing the LIMIT clause.
hi,
i found this on the mysql homepage
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
but i dont see how i can use that to display the last 10 records.
it would be fine if i had a max of 100 records but records are being added all the time
ignore my last comment, being a bit slow today lol
cheers guys :)
vinyl-junkie
03-13-2006, 01:23 AM
If you want to display the last 10 entries in a table in ascending order, here's one way of doing it.
$row_count = mysql_num_rows(mysql_query("SELECT * FROM tblname")) - 10;
$result = mysql_query("
SELECT *
FROM tblname
LIMIT ".$row_count.",10") or die("error!");
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.