我正在尝试找到一个解决方案,让我的PHP脚本只显示连续的最后3个条目,但我能找到的是如何显示第一个条目而不是最后的3.任何想法?
这是显示条目的脚本:
<?php
// Grab the data from our people table
$sql = "select * from people";
$result = mysql_query($sql) or die ("Could not access DB: " . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo "<div class=\"picture\">";
echo "<p>";
// Note that we are building our src string using the filename from the database
echo "<img src=\"content/uploads/" . $row['filename'] . "\" alt=\"\" /><br />";
echo $row['fname'] . " " . "<br />" . "<br />" . $row['lname'] . "<br />";
echo "</p>";
echo "</div>";
}
?>
答案 0 :(得分:5)
试试这个
$sql = "SELECT * FROM people ORDER BY id DESC LIMIT 3";
id=>
是您的列名,您可能需要更改此
并且不要使用mysql
函数,因为它们已被删除,很快就会被删除。了解mysqli
或PDO
答案 1 :(得分:3)
只需反向ORDER BY
,即。如果您的表格按id
列排序,请执行ORDER BY id DESC
然后使用LIMIT 3
:
$sql = "select * from people ORDER BY id DESC LIMIT 3";
PS, mysql _ * 已被弃用......