[SOLVED] displaying guestbook comments from newest to oldest

How do I change the order of the comments entered in a guestbook from newest to oldest entered? Right now it’s displaying the oldest comments first. Here is my code:


<?php // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect server "); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); while($rows=mysql_fetch_array($result)){ ?>
Name : <? echo $rows['name']; ?>
Email : <? echo $rows['email']; ?>
Date/Time : <? echo $rows['datetime']; ?>
Comment : <? echo $rows['comment']; ?>

<? } mysql_close(); //close database ?> [/php]

Thanks for your help.

Admin Edit: Added [PHP] Code tags for readability. Please refer to http://phphelp.com/guidelines.php for posting guidelines

Add an "ORDER BY datefield " to the end of your SQL Statement.

SELECT * FROM $tbl_name ORDER BY datefield

With an “Optional” ASC (Ascending) or DSC (Descending) at the end.

datefield would be an actual field in your database that has some sort of stamp that would allow for a chronological sort. Even an ID could be used presuming an smaller id is earlier and a larger one is later.

Thanks again for your help. My problem has been corrected.

Sponsor our Newsletter | Privacy Policy | Terms of Service