0

I am new at Php and i got this code and if there is no post about selected category i want show warning message. I ve tried "mysqli_num_row" but i did not work. Thank you from now : )

 <?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 } }?>

These are all my codes.

Erdem Nayir
  • 85
  • 2
  • 9
  • It is use as `$num_rows =mysqli_num_rows($select_all_posts_query); if($num_rows >0){---}` – Saty Mar 17 '16 at 12:36
  • Simply `if(mysqli_num_rows($select_all_posts_query) > 0)` would work – Sougata Bose Mar 17 '16 at 12:37
  • 1
    `if($num_rows < 0){` just this – devpro Mar 17 '16 at 12:42
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). – Jay Blanchard Mar 17 '16 at 12:51

3 Answers3

3

mysqli_num_rows - Returns the number of rows in the result set.

No need to use $num_rows.count()

Use:

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'/>";
2

Don't know what is it

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

But you are already using mysqli_num_rows() so just replace above mentioned line with:

if($num_rows <= 0){
devpro
  • 16,184
  • 3
  • 27
  • 38
1

Or to save an unnecessary variable assignment you could do this

Note I used <= less than or equal and not just < If the query returns no results it will be zero, not something less than zero.

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{
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149