Page 1 of 1

echoing last topic posted in my forum

Posted: Tue May 13, 2008 6:05 pm
by saffron
Can some one expand on this for me as I just want the last topic that was posted in forum to echo or print not all the posts.Using Mysql database.

$query = "SELECT body FROM my table";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))

{
echo " {$row['body']} <br>";
}
?>

Thanks.

Re: echoing last topic posted in my forum

Posted: Tue May 13, 2008 6:10 pm
by JacobT
Hey saffron,

You can add some sorting to your SQL statement along with a LIMIT condition:

Code: Select all

SELECT body FROM my table ORDER BY body DESC LIMIT 1
The ORDER BY code says to sort that column and the DESC means descending (in reverse order). The LIMIT 1 just means to only grab one. You can also use LIMIT #, # .. where the first # means the starting number and the second # means the number of records to grab. So LIMIT 2,3 means start at record 2 and grab 3 total, which means records 2-4.

I hope this helps! Good Luck!