[英]PHP print MySQL query in descending order
代碼 :
<?php
$db=mysqli_connect("mysql17.000webhost.com","username","pwd","db");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysql_connect_error();
}
$result = mysqli_query($db,"SELECT * FROM ChatBox");
Print "<div style='text-align:center;'>";
Print "<table border cellpadding=3 style='text-align:left;width:auto;left:auto;right:auto;display:inline-block;'>";
while($info = mysqli_fetch_array( $result ))
{
Print "<tr>";
Print "<th>User : </th> <td>".$info['User'] . " </td>";
Print "<th>Content : </th> <td>".$info['Content'] . " </td></tr>";
}
Print "</table></div>";
使用此代碼,它將按照添加內容的時間順序打印它,我想要的是按照添加內容時間的相反順序打印它。
例如
添加內容之前
D b:
User | Content
ABC | Hello1
DEF | Hello2
添加內容后
User | Content
ABC | Hello1
DEF | Hello2
GHI | Hello3
使用此代碼它將打印
User : ABC
Content : Hello1
User : EFG
Content : Hello2
User : GHI
Content : Hello3
以及將內容添加到 sql 數據庫后預期打印的內容:
User : GHI
Content : Hello3
User : EFG
Content : Hello2
User : ABC
Content : Hello1
“使用此代碼,它會按照添加內容的時間順序打印,我想要的是按照添加內容的時間相反的順序打印它。”
然后您需要做的是創建一個帶有日期/時間值的列,並根據該列對其進行排序; 如果這就是你要問的。 如果沒有,請繼續下面的回答。
首先,你混合了兩個 MySQL API
mysql_connect_error()
應該讀作mysqli_connect_error()
--- 使用mysql_connect_error()
不會給你任何錯誤信息,如果有的話。
另外,您在"username
(作為旁注) "username
缺少引號。
使用ORDER BY
column DESC
<?php
$db=mysqli_connect("xxx","xxx","xxx","xxx");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($db,"SELECT * FROM ChatBox ORDER BY User DESC");
Print "<div style='text-align:center;'>";
Print "<table border cellpadding=3 style='text-align:left;width:auto;left:auto;right:auto;display:inline-block;'>";
while($info = mysqli_fetch_array( $result ))
{
Print "<tr>";
Print "<th>User : </th> <td>".$info['User'] . " </td>";
Print "<th>Content : </th> <td>".$info['Content'] . " </td></tr>";
}
Print "</table></div>";
編輯:
這是從我的數據庫中打印出來的,來自測試。 (按降序打印)
User : Louise Content : Sicilly User : Larry Content : Italy User : FRED Content : Planet Earth User : BOB Content : Spain
這可以通過在 SQL 查詢中使用ORDER BY
關鍵字來完成。 要將結果打印到屏幕上,您可以使用 echo。
為了在你的 PHP 代碼中呈現 HTML,如果你寫這樣的東西可能更容易閱讀:
<?php
$db = mysqli_connect("mysql17.000webhost.com","username", "pwd", "db");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($db, "SELECT * FROM ChatBox ORDER BY User DESC");
?>
<div style='text-align:center;'>
<table border cellpadding=3 style='text-align:left;width:auto;left:auto;right:auto;display:inline-block;'>
<?php while($info = mysqli_fetch_array( $result )): ?>
<tr>
<th>User : </th> <td><?php echo $info['User']; ?></td>
<th>Content : </th> <td><?php echo $info['Content']; ?></td></tr>
<?php endwhile; ?>
</table></div>
通過簡單地使用Order by
mysqli_query($db,"SELECT * FROM ChatBox ORDER BY User,Content DESC; ");
並使用echo
打印數據
echo - 可以輸出一個或多個字符串
打印- 只能輸出一個字符串,並且總是返回 1
echo "<div style='text-align:center;'>";
echo "<table border cellpadding=3 style='text-align:left;width:auto;left:auto;right:auto;display:inline-block;'>";
while($info = mysqli_fetch_array( $result ))
{
echo "<tr>";
echo "<th>User : </th> <td>".$info['User'] . " </td>";
echo "<th>Content : </th> <td>".$info['Content'] . " </td></tr>";
}
echo "</table></div>";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.