douchun6108 2014-04-11 12:49
浏览 59
已采纳

PHP按降序打印MySQL查询

Code :

<?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>"; 

Using this code, it will print it in the order of the time of the content added, which I want is to print it in the order of opposite of the time of the content added.

e.g.

before adding content
db:

User | Content
ABC  | Hello1
DEF  | Hello2

after adding content

User | Content
ABC  | Hello1
DEF  | Hello2
GHI  | Hello3

using this code it will print

User : ABC
Content : Hello1
User : EFG
Content : Hello2
User : GHI
Content : Hello3

and what is expected to be printed after adding content to sql database :

User : GHI
Content : Hello3
User : EFG
Content : Hello2
User : ABC
Content : Hello1

How can I print this result? Can anybody provide the code?

  • 写回答

3条回答 默认 最新

  • doujie1908 2014-04-11 13:07
    关注

    "Using this code, it will print it in the order of the time of the content added, which I want is to print it in the order of opposite of the time of the content added."

    What you will need to do then, is create a column with a date/time value and sort it based on that column; if that's what you're asking. If not, continue with my answer below.


    Original answer

    First, you're mixing two MySQL APIs

    mysql_connect_error() which should read as mysqli_connect_error() --- Using mysql_connect_error() would not have given you any error message(s), should there be any.

    Plus, you're missing a quote in "username (as a sidenote).

    Use 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>"; 
    

    Edit:

    This is what gets printed from my DB, from testing. (printed in descending order)

    User :  Louise  Content :   Sicilly
    User :  Larry   Content :   Italy
    User :  FRED    Content :   Planet Earth
    User :  BOB     Content :   Spain 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用