2014-06-08 105 views
0

我目前正在創建自己的博客,自定義css從頭開始用PHP構建。我對這門語言還有SQL還很陌生。現在,我在從數據庫查詢的每篇文章中顯示Comments(3)鏈接時遇到問題。我有3個表格:用戶,帖子和評論。PHP SQL,如何查詢帖子中的評論數(針對博客文章)?

USERS

id | username | password | name | email | privileges 

帖子

postid | title | date | content | userid | visible | active 

評論

commentid | c_name | c_email | c_ website | c_date | c_content | approved | postid 

這是我從數據庫中顯示的帖子內容的當前查詢:

$query = connect()->prepare(
    "SELECT * FROM posts JOIN users on posts.userid = users.id WHERE visible = 1 ORDER BY postid DESC"); 
$query->execute(); 

<div id="posts"> 
      <?php 
       while($posts = $query->fetch(PDO::FETCH_ASSOC)) { 
       $id = $posts['postid']; 
       $title = $posts['title']; 
       $date = $posts['date']; 
       $content = $posts['content']; 
       $name = $posts['name']; 
      ?> 
      <div id="entry" class="post-<?php echo $id; ?>">    
       <h1><?php echo "<a href='entry.php?id=".$id."'>" .$title. "</a>"; ?></h1> 
       <div class="entry_content"> 
        <?php echo $content; ?> 
        <p><span class="author"><?php echo $name; ?> on <?php echo $date;?></span></p> 
        //this is where I want to put the "Comments(3)" 
       </div> 
      </div> 
      <?php } ?> 
     </div> 

我試着做下面的查詢,通過找到它的帖子ID INSILDE while循環來檢索註釋的數量。

<?php 
     $query = connect()->prepare("SELECT COUNT(postid) as commmentno FROM comments WHERE postid = :postid"); 
     $query->execute(array(':postid' => $id)); 
     $commentnos = $query->fetch(); 

     echo $commentnos['commentno']; 
    ?> 

但結果結束了,我只有一個帖子顯示了正確的註釋數量......如何在一個查詢中獲得這些結果?

+0

您是否使用相同的$查詢變量來獲取發佈數據和評論?如果是,嘗試將獲取評論數從$查詢的查詢更改爲$ commentNoQuery – Derek

回答

0

你可以試試這個:

$query = connect()->prepare(
"SELECT * FROM posts JOIN users on posts.userid = users.id WHERE visible = 1 ORDER BY postid DESC"); 
$query->execute(); 

<div id="posts"> 
     <?php 
      while($posts = $query->fetch(PDO::FETCH_ASSOC)) { 
      $id = $posts['postid']; 
      $title = $posts['title']; 
      $date = $posts['date']; 
      $content = $posts['content']; 
      $name = $posts['name']; 
     ?> 
     <div id="entry" class="post-<?php echo $id; ?>">    
      <h1><?php echo "<a href='entry.php?id=".$id."'>" .$title. "</a>"; ?></h1> 
      <div class="entry_content"> 
       <?php echo $content; ?> 
       <p><span class="author"><?php echo $name; ?> on <?php echo $date;?></span></p> 
       //this is where I want to put the "Comments(3)" 
      $cquery = connect()->prepare("SELECT COUNT(commentid) as commentno FROM comments WHERE postid='".$id."'"); 
      $cquery->execute(); 
      $result = $cquery->fetch(PDO::FETCH_ASSOC); 
      echo $result['commentno']; 
      </div> 
     </div> 
     <?php } ?> 
    </div> 
+0

啊,好吧,我應該計算紀念,而不是postid。謝謝! –

+0

但正如在另一個答案中提到的,你必須在另一個變量不在'$查詢'因爲它會覆蓋後''查詢' –

1

我相信你已經使用相同的變量名$query獲取發佈數據和評論數量。當您使用相同的變量名$ query $query = connect()->prepare("SELECT COUNT(postid) as commmentno FROM comments WHERE postid = :postid");時,它將覆蓋先前的查詢結果。以便您只有一個發佈顯示正確的評論編號。 試着改變你的代碼

<?php 
     $commentquery = connect()->prepare("SELECT COUNT(postid) as commmentno FROM comments WHERE postid = :postid"); 
     $commentquery->execute(array(':postid' => $id)); 
     $commentnos = $commentquery->fetch(); 

     echo $commentnos['commentno']; 
    ?> 
+0

只是試過這個,現在我得到空白。 –

+0

沒有這張取得評論的數字代碼,它顯示多個帖子或一個帖子?它只將變量名稱從$ query更改爲$ commentquery。 – Derek

2

單個查詢可以工作,如:

SELECT 
    posts.id, posts.title, posts.date, posts.content, posts.name, 
    (SELECT COUNT(postid) FROM comments WHERE postid = posts.id) as commmentno 
FROM posts 
JOIN users on posts.userid = users.id 
WHERE visible = 1 
ORDER BY postid DESC 

然後內部查詢是沒有必要的,因爲在所有的主查詢以字段形式返回評論計數。

+0

謝謝!所以加入評論表是不必要的? –

+1

更正內部選擇不需要連接。 – md5madman

相關問題