Jump to content

Archived

This topic is now archived and is closed to further replies.

H-Fish

retrieve top 5 most recent added entries in sql table?

Recommended Posts

i have a table called tabs, and from this table i want to retrive data from columns:

 

artist & title

BUT

i only want to get the 5 most recent submitted entries

is this possible?

Share this post


Link to post
Share on other sites

do you have a timestamp or id field so the db can determine who is the most recent? if so:

 

select artist, title from table order by id_or_time_field desc limit 5

 

what kind of art site? i\'m interested :D

Share this post


Link to post
Share on other sites

i\'ve got an id field, but do i like put id field 1 in there.....

 

heres a what my table looks like from phpmyadmin

 

sql.gif

 

btw you can see it at www.clansr.co.uk/cs/hctabs

 

it just a little selection of scripts where me and my friends can share guitar tabs

Share this post


Link to post
Share on other sites

If your ID field is auito_increment

 

then

 

select artist, title from table order by id desc limit 5;

 

will give you the 5 records with the highest ID numbers

 

P.

Share this post


Link to post
Share on other sites

thanks but i just need one more little thing....,

will i have to decaler this as a variable / string or just echo mysql_result

 

i\'m at school ahnd havent got the chnace to look at my code so i might just be talking crap to myself

Share this post


Link to post
Share on other sites

cheers i did it with this code

 


  ///////// READS INFO FROM DATABASE  ///////

mysql_connect("localhost","clanrryoy","");

@mysql_select_db($DB_DBName) or die( "Unable to select database");

$query="SELECT artist, title FROM tabs order by id desc limit 5";

$result=mysql_query($query);



$num=mysql_numrows($result);



$i=0;

while ($i < $num) {



$artist=mysql_result($result,$i,"artist");

$title=mysql_result($result,$i,"title");



?>

<? // ADDS READ DATA INTO PAGE ?>

<p> </p>

<div align="center">

<p><? echo "$artist"; ?><br>

   <? echo "$title"; ?> 



<?

++$i;

}





echo "</table>";

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.