2016-04-25 45 views
0

我目前有一个MySQL数据库,其中包含任何留言在我的网页上。在列表的底部如何对这些注释进行排序,以便最近出现在顶部?

<? 

$con = mysql_connect("localhost","theshitp_user","password"); 

if (!$con) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("theshitp_posts", $con); 


$query = "SELECT * FROM `userposts` LIMIT 0 , 30"; 

$comments = mysql_query($query); 


while($row = mysql_fetch_array($comments, MYSQL_ASSOC)) 
{ 

    $comment = $row['comment']; 
    $timestamp = $row['timestamp']; 

    $comment = htmlspecialchars($row['comment'],ENT_QUOTES); 

    echo " <div style='text-align: center; border-style: solid; border-weight: 2px; border-radius: 5px; padding: 10px; margin-bottom: 10px; margin-left: 50px; margin-right: 50px;'> 

     <h3>$comment</h3><br /> 
     $timestamp 
    </div> 
    "; 
} 

mysql_close($con); 

?> 

这个帖子的最新评论:我然后写入下面的脚本,使他们出现在页面上。我如何修改这个脚本使它按照最新的顺序排序评论?

+0

在任何日期时间字段上使用order by。 –

+0

这样做的代码会是什么样子?我对SQL很陌生。 –

回答

0

通过使用在任何日期时间字段顺序,你的情况,我认为它的时间,像:

$query = "SELECT * FROM `userposts` ORDER BY timestamp DESC LIMIT 30"; 

这相对于时间戳降序排列显示30条记录。

0

你可以试试这个查询,你会得到最新条目:

$查询= “SELECT * FROM userposts ORDER BY时间戳DESC LIMIT 0, 30”;

相关问题