I’m trying to display all comments made in certain posting page. All variable calls work fine (including the function timeago) but it only shows the very first comment only.
$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> </p><p> </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>';
I am using the same template in other parts of my page and they work fine :-/
What am I doing wrong? Is the COUNT fine?? Am I using the while($com_row = mysql_fetch_assoc($com_result)) correctly??
Hope you can help me 🙂
THANKS
Your query will select one row for each distinct value of
com_topic. This is due to the use ofGROUP BY. If you omit theGROUP BYclause, then you should also omit the aggregate columnCOUNT(com_topic) as com_top.Otherwise, everything else looks ok.