2014-03-06 35 views
0

我真的很想找人帮忙,而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

哪些列/类型,你对职位表?发布你的结构。 – Prix

+0

您是否在询问有关退回记录的订单? – vee

+0

为什么不能在查询中使用ORDER BY?它简单得多。我也不建议'SELECT * ...''*'对于字段名称来说太混乱以至于不能保持 – bansi

回答

0

你的SQL查询的主键代替postid应该是这样的: SELECT * FROM帖子ORDER BY场DESC

+0

感谢您的提示!有效。 :D(不能投票了,但我会尽我所能) – AirbusAi342

+0

我欢迎你! – saurabhk

0

你需要使用 “ORDER BY”:

$result = mysqli_query($con,"SELECT * FROM posts ORDER BY field DESC"); 

的 “场”,在这种情况下,可能会是 “ID”,因为我假设你在帖子表中使用它作为主键。

0

使用ORDER BY DESC安排中的结果降序排列

SELECT * FROM posts ORDER BY postid DESC; 

posts

+0

你为什么使用两个ORDER BY?请参阅我的回答 – rambossa

+0

这是一个错字..感谢...更新 –

0

除非您指定必须执行排序的字段,否则无法按所需顺序获取结果,而是按默认顺序获取结果。

如果你想通过字段进行排序,说POST_ID,那么,你可以使用这样的:

select * from posts order by post_id desc 

如果你仍然想在其默认位置获取记录,但想从后到前顺序显示,那么您需要选择一个临时行号变量,然后重新排列显示记录。

select 
    @rn:[email protected]+1 as row_num, 
    posts.* 
from 
    posts, 
    (select @rn:=0) tmp_row_nums order by post_id desc