most recent 5 rows
| From: | Justin French | Date: | Sat, 04 Aug 2001 09:52:04 +0000 |
| Subject: | most recent 5 rows | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-61229@lists.php.net to get a copy of this message | ||
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["text"]."<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