RE: [PHP] most recent 5 rows

From: Date: Sat, 04 Aug 2001 10:56:19 +0000
Subject: RE: [PHP] most recent 5 rows
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-61230@lists.php.net to get a copy of this message
Try this SQL Justin: $sql = "SELECT * FROM news DESC LIMIT 5"; Jeff > -----Original Message----- > From: Justin French [mailto:justin@indent.com.au] > Sent: Saturday, August 04, 2001 5:52 AM > To: php > Subject: [PHP] most recent 5 rows > > > Hi all, > > I have a table with the column "id", which is a unique number, > auto-incremented, so the highest "id" is the most recent. > > I wish to retrieve the last five rows from the table (there > will be hundreds of rows) and print some columns to the screen. > > > > I have this table: > id,heading,text,user > > > This is what I have, which retrieves ALL rows, in id order, from > smallest to greatest: > > --- > $db = mysql_connect("localhost", "root", ""); > mysql_select_db("soundpimp",$db); > > $sql = "SELECT * FROM news"; > $sql_result = mysql_query($sql); > > while ($sql_myrow = mysql_fetch_array($sql_result)) > { > echo > > $sql_myrow["id"]."<BR>".$sql_myrow["heading"]."<BR>".$sql_myrow["t > > ext"]."<BR>".$sql_myrow["user"]."<BR>---<BR>"; > } > --- > > What I'd like to do is ONLY echo the highest 5 id's (the latest 5 rows), > in REVERSE ORDER. > > > How do I approch this? Do I find the highest ID, then do 5 SELECTS, > counting back? > > Any code / ideas appreciated. > > > > Jsutin French > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net > > >

« previous php.general (#61230) next »