echoing last topic posted in my forum

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
saffron
Forum Newbie
Posts: 7
Joined: Sat Apr 05, 2008 6:00 pm

echoing last topic posted in my forum

Post 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.
JacobT
Forum Commoner
Posts: 43
Joined: Tue May 13, 2008 11:07 am
Location: Los Angeles, CA

Re: echoing last topic posted in my forum

Post 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!
Post Reply