Forum Moderators: coopster
[quote]
<?
$connection = mysql_connect("localhost" , "username" , "password");
$db = mysql_select_db(database , $connection);
$result=mysql_query("SELECT * FROM news");
$ctr=mysql_num_rows($result);for($i=$ctr;$i>($ctr-4);$i--){
$result = mysql_query("SELECT * FROM news WHERE id='$i'");
$row = mysql_fetch_array($result);
$date = $row["date"];
$content = $row["content"];
echo "<strong>$date</strong>\n";
echo "<p>$content</p>\n";
}
?>
[/quote]
I was wondering if there was a more efficent way to display these results (without doing the backwards for loop method that I am doing, and without having to query the database to get the number of rows). Thanks for any help you can give me.
<?
$connection = mysql_connect("localhost" , "username" , "password");
$db = mysql_select_db(database , $connection);
$result = mysql_query("SELECT * FROM news WHERE id='$i' ORDER BY date DESC");
for($i=1;$i>5;++$i)
{
$row = mysql_fetch_array($result);
echo "<strong>" . $row["date"] . </strong>\n";
echo "<p>" . $row["content"] . "</p>\n";
}
mysql_close($connection);
?>
;)