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
$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.
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++;
}