2011-08-20 83 views
-2

Im abeginner我需要完整的代碼來獲取我的數據庫中的評論數量(如在Facebook中:不是評論,而是每篇文章的評論總數[或者在我的情況下,每個「列表」))在div標籤中或者其他的東西。如何從db獲取評論數量?

這裏是我的數據庫的屏幕截圖:

enter image description here

+1

這個網站是不是得到完整的代碼免費的地方。這是一個問技術問題並獲得答案的地方,以便學習。 –

回答

1
select listing_id,count(*) as total 
from comments 
group by listing_id 
+0

如何打印它,我的意思是回顯它 –

+0

你必須看看mysql_fetch_assoc()並在php中使用while循環。開始閱讀手冊。此外,我不明白,如果你使用PHP或Java。 –

0
$sql = "SELECT * FROM comments"; 
$stmt = $mysqli->prepare($sql); 
$stmt->execute(); 
$stmt->store_result(); 
$checker = $stmt->num_rows; 
$stmt->fetch(); 
$stmt->close(); 

//Checker has the number of rows 

echo $checker; 

其功能NUM_ROWS在這裏你可以閱讀更多

num_rows mysqli

num_rows mysql

編輯:如果你想例如僅從一個文章的評論,你可以做你的SQL像

$sql = "SELECT * FROM comments WHERE articleid = ?";