gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:43:35 26 4
gpt4 key购买 nike

我正在尝试显示在某个发布页面中发表的所有评论。所有变量调用都可以正常工作(包括函数 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)) ?希望你能帮助我:-)

谢谢

最佳答案

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

否则,其他一切看起来都很好。

关于mysql_fetch_assoc 显示 While 循环中的一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7480539/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com