2011-09-20 63 views
0

我試圖顯示在某個發佈頁面中所做的所有評論。所有變量調用都可以正常工作(包括函數timeago),但它只顯示第一個註釋。mysql_fetch_assoc在While循環中顯示一條記錄

$com_sql = "SELECT comments.com_id, 
        comments.com_topic, 
        comments.com_user, 
        comments.com_content, 
        comments.com_date, 
        users.userid, 
        users.username, 
        users.usernombre, 
        COUNT(com_topic) AS com_top 
      FROM comments 
      LEFT JOIN users 
      ON comments.com_user = users.userid 
      WHERE com_topic = " . mysql_real_escape_string($_GET['id']) . " 
      GROUP BY com_topic 
      ORDER BY com_id DESC"; 

$com_result = mysql_query($com_sql); 

if(!$com_result) 
{echo 'The COMMENTS table could not be displayed.';} 
else 
{ if(mysql_num_rows($com_result) == 0){ echo 'No comments yet!';} 
} 

// Now I should show them one by one: 
// Should $com_row be com_top?? 

while($com_row = mysql_fetch_assoc($com_result)) 
{ echo '<table width="100%" height="253" border="4"> 
<tr><td width="9%" rowspan="2" valign="top"><p>IMG</p> 
<p>editar</p><p>&nbsp;</p><p>&nbsp;</p></td><td width="63%">'; 

$com_user = stripslashes($com_row['username']); 
$com_usernombre = stripslashes($com_row['usernombre']); 
echo $com_user; 
echo $com_usernombre; 

echo '</td><td width="4%" height="24"><div align="right"> <p>PM</p> 
</div></td><td width="24%"><div align="right">'; 

$referencedate = strtotime($com_row['com_date']); 
$result = timeago($referencedate); 
echo 'Ago'; 
echo $result ; 

echo '</div></td></tr><tr><td colspan="3" valign="top">'; 

$com_content = stripslashes($com_row['com_content']); 
echo $com_content; 
echo '</td></tr>'; 
} 
echo '</table>'; 

我使用我的網頁的其他部分相同的模板和他們很好地工作: -/ 我在做什麼錯? COUNT很好嗎?我是否正確使用while($ com_row = mysql_fetch_assoc($ com_result))? 希望你能幫助我:-)

THANKS

回答

1

您的查詢將選擇一排爲com_topic每個不同的值。這是由於使用GROUP BY。如果您省略GROUP BY子句,則還應該省略聚合列COUNT(com_topic) as com_top

否則,一切看起來都不錯。

+0

YAY !!!你是個了不起的男孩!我花了整整一天的時間來解決這個問題,但花了10秒鐘才發現: - /感謝李! – cbarg

相關問題