2016-03-17 18 views
0

我是新來的Php,我得到了這段代碼,如果沒有關於所選類別的文章,我想顯示警告消息。我試過「mysqli_num_row」,但我沒有工作。感謝您從現在開始:))我如何在php中使用「mysqli_num_rows」?

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

這些都是我的代碼。

+0

它是用作'$ NUM_ROWS = mysqli_num_rows($ select_all_posts_query); if($ num_rows> 0){---}' – Saty

+0

只要if(mysqli_num_rows($ select_all_posts_query)> 0)'會起作用 –

+1

'if($ num_rows <0){'只是這個 – devpro

回答

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{ 
+0

投給<=建議,良好的工作朋友.. – devpro

+0

@devpro爲什麼謝謝你先生 – RiggsFolly

+1

@devpro但仍然接受'<'的答案。有時我想知道爲什麼我們打擾。 – RiggsFolly

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

謝謝,它的工作原理:)祝你有美好的一天! –

+2

它必須是:'if($ num_rows <= 0)' - 否則你的條件永遠不會成爲'true'。因爲您將有0行或多於0行,但從不少於0. –

+0

我應該編輯您的錯誤,還是您?由於這已被接受,如果它不包含錯誤,它將是有用的。 – RiggsFolly

3

不知道它是什麼

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

但你已經在使用mysqli_num_rows()所以只需更換上述行與:

if($num_rows <= 0){ 
+0

哦,我沒有看到你編輯過你的答案,我懇求你原諒你複製你。 – RiggsFolly

+1

*「不知道它是什麼」* - 他們可能在腦海中發生了一些事情,他們認爲他們可以使用num_rows()並同時計數。 *誰知道*通過人們的頭腦有時會發生什麼;) –

+0

@ Fred-ii-:是的,你的權利,:) – devpro