我怎样才能使用" mysqli_num_rows"在PHP?

时间:2016-03-17 12:35:10

标签: php

我是Php的新手,我得到了这段代码,如果没有关于所选类别的帖子,我想要显示警告信息。我试过" mysqli_num_row"但我没有工作。谢谢你从现在开始:))

 <?php
                        if(isset($_GET['category'])){
                            $post_category_id = $_GET['category'];
                        }

                        $query = "SELECT * FROM posts WHERE post_category_id = $post_category_id ";
                        $select_all_posts_query = mysqli_query($connection, $query);
                        $num_rows = mysqli_num_rows($select_all_posts_query);

                        if($num_rows.count() < 0){
                                echo "<h1 class='text-center'>THERE IS NO POST ABOUT THIS CATEGORY!</h1>";
                                echo "<img style='margin-left:225px;' width='300' class='img-responsive' src='images/sorry.png'/>";
                        }else{
                        while($row = mysqli_fetch_assoc($select_all_posts_query)){
                            $post_id = $row['post_id'];
                            $post_title = $row['post_title'];
                            $post_author = $row['post_author'];
                            $post_date = $row['post_date'];
                            $post_image = $row['post_image'];
                            $post_content = substr($row['post_content'], 0, 50);           
                            ?>
                        <h1 class="page-header">
                Page Heading
                <small>Secondary Text</small>
            </h1>
                        <!-- First Blog Post -->
                        <h2>
                <a href="post.php?p_id=<?php echo $post_id; ?> "><?php echo $post_title ?></a>
            </h2>
                        <p class="lead">
                            by
                            <a href="index.php">
                                <?php echo $post_author ?>
                            </a>
                        </p>
                        <p><span class="glyphicon glyphicon-time"></span>
                            <?php echo $post_date ?>
                        </p>
                        <hr>
                        <a href="post.php?p_id=<?php echo $post_id; ?>"><img width="600" class="img-responsive" src="images/<?php echo $post_image;?>" alt=""></a>
                        <hr>
                        <p>
                            <?php echo $post_content ?>
                        </p>
                        <a class="btn btn-primary" href="#">Read More <span class="glyphicon glyphicon-chevron-right"></span></a>
                        <hr>
                        <?php } }?>

这些都是我的代码。

3 个答案:

答案 0 :(得分:3)

  

mysqli_num_rows - 返回结果集中的行数。

无需使用$num_rows.count()

使用:

if($num_rows <= 0){

    echo "<h1 class='text-center'>THERE IS NO POST ABOUT THIS CATEGORY!</h1>";
    echo "<img style='margin-left:225px;' width='300' class='img-responsive' src='images/sorry.png'/>";

答案 1 :(得分:3)

不知道它是什么

if($num_rows.count() < 0){

但您已经在使用mysqli_num_rows(),所以只需将上面提到的行替换为:

if($num_rows <= 0){

答案 2 :(得分:3)

或者为了保存不必要的变量赋值,你可以这样做

  

注意我使用<=小于或等于而不只是<如果查询没有返回结果,则它将为零,而不是小于零。

if(mysqli_num_rows($select_all_posts_query) <= 0){
   echo "<h1 class='text-center'>THERE IS NO POST ABOUT THIS CATEGORY!</h1>";
   echo "<img style='margin-left:225px;' width='300' class='img-responsive' src='images/sorry.png'/>";
}else{