-2

我使用 PHP 和 MySQL 制作了一个微博系统(如 Twitter)。每次有人发布一个项目时,它都会在数据库中包含 UNIX 时间戳。但我的问题是,我如何获得页面顶部的最新项目,第二个到最新的第二个等?所以基本上,在页面顶部具有“最高”时间戳的那个,在那个下面有一点“更少”时间戳的那个。这就是我得到的(我知道 mysql_ 函数已被弃用):

<?php
/*Database test.
© 2013 Sydcul. All rights reserved.

To set the database settings, use the 'install' directory*/

include ($_SERVER['DOCUMENT_ROOT'] . '/test/config.inc.php');

$connection = mysql_connect($host, $user, $password);

mysql_select_db($database, $connection);

$messages = mysql_query("SELECT * FROM `" . $prefix . "microblog`");

while($row = mysql_fetch_array($messages))
  {
    $names = mysql_query("SELECT first_name,last_name FROM `" . $prefix . "data` WHERE id='" . $row['id'] . "'");
    $name = mysql_fetch_assoc($names);
    $fullname = $name['first_name'] . " " . $name['last_name'];
    echo "<h2>Posted by " . $row['id'] . " (" . $fullname . ")</h2>";
    echo $row['message'] . "<br><br>";
  }
mysql_close($connection);
?>

请解释一下如何做,而不是只给我代码,否则我和stackoverflow上的其他人无法从中学习:)

4

2 回答 2

5

ORDER BY在您的 SQL 中使用

SELECT * FROM tablename ORDER BY datefield DESC
于 2013-01-22T16:03:18.560 回答
0

如果您有一个 auto_incrementing ID 字段,请获取您的数据和 ORDER BY ID,否则如果您有一个 DateField,则按降序排序

ORDER BY `ID` DESC

就如此容易。

于 2013-01-22T16:08:59.840 回答