我想回显来自comments
表的行,其中entries
表上存在注释。但是如果评论是空的,则不要显示它们。我下面的代码目前会显示对博客条目的评论,但如果为空则不会显示。 换句话说:博客条目和评论都会显示出来,但是当您回显条目但没有评论时甚至不会回显任何内容其他的echo语句甚至不起作用。我很确定我做错了。我有一个代码,我做了。
//if a blog post was made
//echo all the blog post
//If you select a particular blog post
//Echo that particular blog post
if (!empty($postID)) {
$command = "select t1.blogID t1blogID, t1.author t1author, t1.date t1date,
t1.entry, t2.commentID, t2.author t2author, t2.date t2date,
t2.comments from $table_name t1, $table_name2 t2
where t1.blogID = t2.blogID and t1.blogID = $postID";
$result = $db->query($command);
while ($data = $result->fetch_object()) {
// I figured I'd create a variable here
$postID = $data->t1blogID;
$t1author = $data->t1author;
$t1date = $data->t1date;
$entry = $data->entry;
$commentID = $data->commentID;
$t2author = $data->t2author;
$t2date = $data->t2date;
$comments = $data->comments;
//So I can create this conditional statement
// if there is no comment
if (!empty($commentID)) {
echo "<TR><TD>Blog ID ".$postID."</TD>";
echo "<TD>".$t1author."</TD>";
echo "<TD>".$t1date."</TD>";
echo "<TD>".$entry."</TD></TR>\n";
echo "<TR><TD>Comment ID ".$commentID."</TD>";
echo "<TD>".$t2author."</TD>";
echo "<TD>".$t2date."</TD>";
echo "<TD>".$comments."</TD></TR>\n";
}
else {
echo "<TR><TD>Blog ID ".$postID."</TD>";
echo "<TD>".$t1author."</TD>";
echo "<TD>".$t1date."</TD>";
echo "<TD>".$entry."</TD></TR>\n";
}
}
result->free();
答案 0 :(得分:0)
尝试左连接两个表:
select t1.blogID t1blogID, t1.author t1author, t1.date t1date, t1.entry, t2.commentID, t2.author t2author, t2.date t2date, t2.comments from $table_name t1 LEFT JOIN $table_name2 t2 ON t1.blogID = t2.blogID where t1.blogID = $postID
会起作用。
答案 1 :(得分:0)
我不能发表评论所以我会在这里做。 我不确定你的SQL是否正确它不应该是一个JOIN?
但很明显$ commentID不为空,请尝试:
if($commentID > 0)