我真的在寻求帮助,Read from bottom to top in PHP & SQL对我没有帮助;我不做MySQL。
我基本上要做的是从下到上阅读数据库表。与目前从上到下读取的事实不同,我希望从底部开始。是否有PHP函数或代码而是从底部开始读取?
以下是读取数据库的代码:
<?php
//Here is the connecting to database stuff; works fine and not going to share it
$result = mysqli_query($con,"SELECT * FROM posts");
//This has PHP select and read the table (at least I think it does; I'm not the best coder in the world)
while($row = mysqli_fetch_array($result))
{
echo "<h4>";
echo $row['username'];
echo "</h4><p>";
echo $row['message'];
echo "</p>";
}
//All this stuff just reads and echos the data
mysqli_close($con);
//Closes database connection
?>
答案 0 :(得分:0)
您需要使用“ORDER BY”:
$result = mysqli_query($con,"SELECT * FROM posts ORDER BY field DESC");
在这种情况下,“字段”可能是“id”,因为我假设您将它用作帖子表中的主键。
答案 1 :(得分:0)
使用ORDER BY DESC
按降序排列结果集
SELECT * FROM posts ORDER BY postid DESC;
将postid
替换为posts
table
答案 2 :(得分:0)
您的SQL查询应该是这样的: SELECT * FROM posts ORDER BY字段DESC
答案 3 :(得分:0)
除非您指定必须执行排序的字段,否则无法按所需顺序获取结果,而是默认值。
如果你想按字段排序,比如post_id,那么你可以使用:
select * from posts order by post_id desc
如果你仍想在默认位置获取记录但想要从最后一个顺序显示,那么在选择时需要一个临时行号变量,然后重新排序显示记录。
示例 :
select
@rn:=@rn+1 as row_num,
posts.*
from
posts,
( select @rn:=0 ) tmp_row_nums order by post_id desc