PHP& MySQL - 更改代码ORDER BY

时间:2014-12-15 17:54:51

标签: php html mysql database

嗨,我的MySQL数据库没什么问题 我在我的数据库中有这些东西:enter image description here

当我有代码时:

$show_name = "SELECT name FROM serverlist_comments WHERE location = 1";
$result = mysqli_query($conn,$show_name);
$show_carriage = "SELECT carriage FROM serverlist_comments WHERE location = 1 ORDER BY datetime";
$result_carriage = mysqli_query($conn,$show_carriage);
while($row_name = mysqli_fetch_assoc($result)){
while($row_carriage = mysqli_fetch_assoc($result_carriage)){
       print($row_name['name']);
       echo " napísal/a toto : ";
       print($row_carriage['carriage']);
       echo "<br/>";
}
}
?>

我将收到输出:
“Kubajsk0napísal/ a toto:ok
Kubajsk0napísal/ a toto:niet
Kubajsk0napísal/ a toto:ale“

我知道这是一切正常的日期/时间18:17:29(因为“ok”)是第一次,日期/时间18:17:32(如“niet”)来之后登记/>

但是我希望只有这个输出的对立面 我希望输出为:
“Kubajsk0napísal/ a toto:ale
Kubajsk0napísal/ a toto:niet
Kubajsk0napísal/ a toto:ok“

谢谢你的每一个答案:D

3 个答案:

答案 0 :(得分:2)

您应该为查询添加方向: SELECT carriage FROM serverlist_comments WHERE location = 1 ORDER BY datetime DESC

DESC表示下降,ASC表示上升。

答案 1 :(得分:2)

可能是降序,如:

ORDER BY datetime DESC

或:

ORDER BY datetime ASC

升序

答案 2 :(得分:2)

我不明白为什么你有两个循环。如果你想获得如下输出:

Kubajsk0 napísal/a toto : ale
Kubajsk0 napísal/a toto : niet
Kubajsk0 napísal/a toto : ok

您可以使用以下代码。

$show_carriage = "SELECT name, carriage FROM serverlist_comments WHERE location = 1 ORDER BY datetime DESC";
$result_carriage = mysqli_query($conn,$show_carriage);
while($row_carriage = mysqli_fetch_assoc($result_carriage)){
       echo $row_carriage['name'];
       echo " napísal/a toto : ";
       echo $row_carriage['carriage'];
       echo "<br/>";
}