mysql_fetch_assoc在While循环中显示一条记录

时间:2011-09-20 05:27:51

标签: mysql fetch

我正在尝试显示在某个发布页面中发表的所有评论。所有变量调用都可以正常工作(包括函数timeago),但它只显示第一个注释。

$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))吗? 希望你能帮助我: - )

感谢

1 个答案:

答案 0 :(得分:1)

您的查询将为com_topic的每个不同值选择一行。这是由于GROUP BY的使用。如果省略GROUP BY子句,则还应省略聚合列COUNT(com_topic) as com_top

否则,其他一切看起来都不错。