2015-10-16 47 views
1

我正在嘗試創建一個討論板,並試圖在每個論壇中顯示主題數和總帖子數。我認爲這個問題存在於我的WHERE條款的某處,因爲之前的問題導致我遇到問題。PHP MySQL - 從select語句中計數行數

我知道rowCount()不應該依賴,並且在我的情況下無法正常工作,主要是因爲它不適用於SELECT語句。

我已經看過SELECT count(*)陳述以及做$row = $stmt->fetchAll(); $numrows = count($row)但是,我沒有得到他們任何一個工作。

<?php 
     /* CATEGORIES */ 
      /* SELECTS ALL OF THE CATEGORIES OF THE BOARD */ 

      $query = "SELECT * FROM bkg_categories"; 

      try { 
       $stmt = $db->prepare($query); 
       $result = $stmt->execute(); 
      } catch(PDOException $e) { 
       $error[] = "An error has occured. Please try again later."; 
      } 
      $categories = $stmt->fetchAll(); 

     /* FORUMS */ 
      /* SELECT ALL OF THE FORUMS IN EACH CATEGORY FOR THE BOARD */ 
      foreach($categories as $category) { 
       $catid = $category['category_id']; 

       $query = "SELECT * FROM bkg_forums WHERE category_id = :catid"; 
       $query_params = array(':catid' => $catid); 

       try { 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
       } catch(PDOException $e) { 
        $error[] = "An error has occured. Please try again later."; 
       } 
       $forums[$catid] = $stmt->fetchAll(); 

       foreach($forums[$category['category_id']] as $forum) { 
           $query = "SELECT * FROM bkg_topics where forum_id = :forumid"; 
           $query_params = array(':forumid' => $forum['forum_id']); 

           try { 
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
           } catch(PDOException $e) { 
            $error[] = "An error has occured. Please try again later."; 
           } 
           $rows3['forumid'] = $stmt->fetchAll(); 
           $rowCount = count($rows3['forumid']); 
           echo $rowCount; 
           var_dump($rowCount); 
          } 



      } 
     /* END QUERIES */ 
    ?> 

這些都是我正在使用的所選查詢。在我的PHP select查詢的最後一個foreach循環中,你可以看到我在玩什麼,var_dump()echo的作品,當它不在任何foreach循環內,除了初始父循環之外,但是一旦我將它移到我的表,它似乎沒有工作。

<?php foreach($categories as $category): ?> 
       <table class="forumtable"> 
        <tr> 
         <td colspan="2"><strong><?php echo $category['category_name']; ?></strong></td> 
         <td><strong>Lastest Post</strong></td> 
         <td><strong>Topics</strong></td> 
         <td><strong>Posts</strong></td> 
        </tr> 
       <?php foreach($forums[$category['category_id']] as $forum): ?> 
        <tr> 
         <td width="5%" class="forum"></td> 
         <td class="forum no-padding"> 
          <a href="viewforum.php?f=<?php echo $forum['forum_id']; ?>"><?php echo $forum['forum_name']; ?></a> 
          <div class="description"><?php echo $forum['forum_desc']; ?></div> 
         </td> 
         <td width="15%" class="forum content">  
          <?php if($forum['forum_last_post_topic_id'] == 0): ?> 
           <div>No posts...</div> 
          <?php else: ?> 
           <?php if($forum['forum_last_post_id'] == 0): ?> 
            <a href="viewtopic.php?f=<?php echo $forum['forum_id']; ?>&t=<?php echo $forum['forum_last_post_topic_id']; ?>"><?php echo substr($forum['forum_last_post_title'], 0, 10); ?></a> 
           <?php else: ?> 
            <a href="viewtopic.php?f=<?php echo $forum['forum_id']; ?>&t=<?php echo $forum['forum_last_post_topic_id']; ?>#<?php echo $forum['forum_last_post_id']; ?>"><?php echo substr($forum['forum_last_post_title'], 0, 10); ?></a> 
           <?php endif; ?> 
          <?php endif; ?> 
         </td> 
         <td width="5%" class="forum content"> 
          <?php 
          echo $forum['forum_topics']; ?></td> 
         <td width="5%" class="forum content"><?php 
          echo $forum['forum_posts']; ?></td> 
        </tr> 
        <?php endforeach; ?>      
       </table> 
      <?php endforeach; ?> 
+0

你在哪裏填寫'$ forum ['forum_topics'];''和'$ forum ['forum_posts'];'? –

+0

您在這裏只需要一個查詢,並且從不在循環內執行查詢。 – Strawberry

+0

@mapek這只是我在測試我是否在我發佈之前無法弄清楚的。草莓,如果我不應該在循環內執行查詢,我將如何去做我所做的事情?在我之前的問題中,我沒有被告知從不在循環中執行查詢。 http://stackoverflow.com/questions/32976391/php-mysql-creating-a-discussion-board – Moonblaze

回答

0

我終於可以得到選擇(*)的工作。

$db->query('SELECT COUNT(*) FROM users')->fetchColumn();是我以前得到我需要的工作。