PHP无法从MySQL数据库中获取数据

时间:2015-02-11 18:49:57

标签: php mysql

我正在制作一个评论框,其中

  • 评论框将在帖子下
  • 每个评论框都有与帖子相同的ID
  • 这就是我喜欢做while循环
  • 的原因

但问题是

  • 由于我使用fetch_assoc()方法,因此某些注释表将为空 我为他们制作了一个if else代码

这是我的if else代码:

$select_comment_table = "SELECT * FROM feed_comment_" . $row['id'] . "ORDER BY id";
$result_query_select_comment_table = $dbc->query($select_comment_table);
if(!$result_query_select_comment_table) {
    $result_select_comment_table = array("full_name" => "", "comment"=> "No comments yet.");

}
else {
    $result_select_comment_table = $result_query_select_comment_table->fetch_assoc();
}

如果需要其他代码:

echo '<div id="feed_comment_box_' . $row['id'] . '"' . 'class="feed_comment_box_cl"><div id="add_comment_id" class="add_comment_cl">
<form class="comment_form" method="post" action="' .$_SERVER['PHP_SELF']  . '">
<input name="comment_full_name" type="text" class="input_comment_full_name">  </input> 
<textarea name="input_comment_text" type="text" class="input_comment_text" ></textarea><input name="comment_submit" type="submit"></input> <br>
 </form>
</div><br>
<div id="comment_box_id" class="comment_box_cl">
<table tabindex="0" class="comment_box"> 
<tr> <td class="comment_full_name">' . $result_select_comment_table["full_name"] . '</td></tr><br>' . 
'<tr> <td class="comment_full_name">' . $result_select_comment_table["comment"] . '</td></tr><br>'
. '</table></div></div>';
echo '</div>';

问题:当你看到if else代码时,如果没有评论,php应该回显no comments yet。但是,即使插入注释(我试图手动插入数据库),它显示no comments yet,但它应显示注释

注意:我正在使用while循环,这就是为什么评论div将在每个帖子下面。

1 个答案:

答案 0 :(得分:3)

您的查询中似乎缺少空格:

$select_comment_table = "SELECT * FROM feed_comment_" . $row['id'] . "ORDER BY id";

应该是

$select_comment_table = "SELECT * FROM feed_comment_" . $row['id'] . " ORDER BY id";

无效查询导致$ result_query_select_comment_table始终为false。

相关问题