Gossamer Forum
Home : General : Databases and SQL :

sql query to select last 5 entries

Quote Reply
sql query to select last 5 entries
Hi guys,

I'm a mySQL n00b and had a quick question. I am building a simple news section whilst learning PHP/mySQL and was wondering if I can do a simple SQL query to select the last 5 entries in the database. Is there any way of counting how many rows exist in the database and decrementing some counter to display the last 5 entries from the last one.

At the moment I am doing it in a very "inelegant" way which does not reallyt solve my problem anyway.
Here's my PHP
Code:
// Request the text of all the news items
$result = mysql_query('SELECT newsText FROM newsitems');
if (!$result) {
die('<p>Error performing query: '.mysql_error().'</p>');
}
// Display the text of each news item in a paragraph
$min = 1;
$max = 5;
$i = 0;
while ( $row = mysql_fetch_array($result) ) {
if($i >= $min && $i <= $max) {
echo('<p>'.$row['newsText'].'</p>');
}
$i++;
}
TIA.
Subject Author Views Date
Thread sql query to select last 5 entries aragee 17359 Oct 7, 2003, 7:49 AM
Post Re: [aragee] sql query to select last 5 entries
Andy 17161 Oct 7, 2003, 8:24 AM
Thread Re: [aragee] sql query to select last 5 entries
cornball 17124 Oct 7, 2003, 10:39 PM
Thread Re: [cornball] sql query to select last 5 entries
aragee 17087 Oct 24, 2003, 12:15 AM
Post Re: [aragee] sql query to select last 5 entries
Alex 16929 Oct 24, 2003, 9:47 AM