如何以降序重新排序整个sql语句?

时间:2018-09-13 05:13:16

标签: php mysql

我有此代码,我想按降序显示数据。我使用ORDER BY postID。但这并没有显示错误。请帮助我解决此问题。

  

警告:mysqli_num_rows()期望参数1为mysqli_result,在第13行的C:\ xampp \ htdocs \ mysite \ category.php中给出布尔值

这是我的代码 $ post_sql =“ SELECT post.postID,post.head,post.author,post.date,post.content1,post.content2,post.content3,post.image,post.figure1,post.figure2,post.figure3, post.link1,post.link2,category.name AS catname来自postID,按postID加入类别,在post.categoryID = category.categoryID上,post.categoryID =“。$ _ GET ['categoryID'];

if ($post_query=mysqli_query($dbconnect,$post_sql)) {
    $post_rs=mysqli_fetch_assoc($post_query);
}
if (mysqli_num_rows($post_query)==0) {
    echo "Not Found !!! ";
}else{
    ?>
    <h1><?php echo $post_rs['catname']; ?></h1>

    <div class="col-md-8">
        <?php
        if($_GET['categoryID']==4){
            ?>
            <h4> &nbsp Upload your project and Events ... share the experience</h4>
            <h5> &nbsp &nbsp &nbspThis area is for you to share your projects with others. You can upload some photos videos and related documents of your project. Also help others to make it!!<br><br>
             &nbsp &nbsp &nbsp Also let us know about the events and exhibitions in your school or university. Just click below!!</h5>
            <a href="index.php?page=addyours"><button>Upload yours</button></a>
            <br><hr>
            <?php
        }
    ?>
    <?php do{
            ?>
            <div class="col-md-6">
                <div class="post">
                    <a class="post-img" href="index.php?page=post&postID=<?php echo $post_rs['postID'];?>"><img src="admin/images/<?php echo $post_rs['image'];?>" alt="" width=400 height=275></a>
                    <div class="post-body">
                        <div class="post-category">
                            <a href="#"><?php echo $post_rs['catname']; ?></a>
                        </div>

                        <h3 class="post-title">
                            <a href="index.php?page=post&postID=<?php echo $post_rs['postID'];?>">
                            <?php echo $post_rs['head']; ?></a></h3>
                        <ul class="post-meta">
                            <li><a href="author.php"><?php echo $post_rs['author']; ?></a></li>
                            <li><?php echo $post_rs['date']; ?></li>
                        </ul>
                        <p><?php echo $post_rs['content1']; ?></p>
                    </div>
                </div>
            </div>
    <?php
    }while($post_rs=mysqli_fetch_assoc($post_query));
    ?>
    </div>
    <?php
}

?>`

2 个答案:

答案 0 :(得分:0)

由于错误提示您的查询执行失败,并且order by子句必须在where条件之后

$post_sql="SELECT post.postID, post.head, post.author, post.date, post.content1, post.content2, post.content3, post.image, post.figure1, post.figure2, post.figure3, post.link1, post.link2, category.name AS catname FROM post order by postID JOIN category ON post.categoryID=category.categoryID WHERE post.categoryID=".$_GET['categoryID'];

将其更改为这样

$post_sql="SELECT post.postID, post.head, post.author, post.date, post.content1, post.content2, post.content3, post.image, post.figure1, post.figure2, post.figure3, post.link1, post.link2, category.name AS catname FROM post  JOIN category ON post.categoryID=category.categoryID WHERE post.categoryID=".$_GET['categoryID']."order by postID desc";

答案 1 :(得分:0)

我不了解PHP,但是看来您必须在所示代码的第4行(或文件的第13行)中编写if (mysqli_num_rows($post_rs)==0) {才能删除警告。另外,order by子句通常出现在查询的末尾,而不是联接之前。