Jump to content


Photo

retrieve top 5 most recent added entries in sql table?


  • Please log in to reply
5 replies to this topic

#1 H-Fish

H-Fish
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 16 February 2003 - 12:39 PM

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?

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 16 February 2003 - 06:18 PM

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
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 H-Fish

H-Fish
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 17 February 2003 - 09:45 AM

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

Posted Image

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

#4 pallevillesen

pallevillesen
  • Members
  • PipPipPip
  • Advanced Member
  • 135 posts
  • LocationDenmark

Posted 17 February 2003 - 01:04 PM

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.
Palle Villesen, www.birc.dk [br]Bioinformatics Research Center

#5 H-Fish

H-Fish
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 17 February 2003 - 01:55 PM

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

#6 H-Fish

H-Fish
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 17 February 2003 - 06:03 PM

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>&nbsp;</p>

<div align="center">

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

    <? echo "$title"; ?> 



<?

++$i;

}





echo "</table>";






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users