我是MySQLi和PHP的新手,我试图通过匹配postid从我的数据库中选择注释。它应该选择具有该id的所有行,并显示每个行的名称和值。但不工作。PHP MySQLi:选择多行并显示每个选择的列值
// Create connection
$conn = mysqli_connect($hostname, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
echo "<script>alert('dead!')</script>";
}
$get_comments = "SELECT * FROM 'articlecomments' WHERE 'commentpost' = 'post1' LIMIT 0 , 30";
$check_comments = mysqli_query($conn, $get_comments);
while ($row = mysqli_fetch_assoc($check_comments)) {
echo "<script>alert('we have comments!');</script>
<div class='comment-single'>
<div class='row'>
<div class='col-md-3'>
" . $check_comments['commentname'] . "
</div>
<div class='col-md-9'>
" . $check_comments['commentvalue'] . "
</div>
</div>
</div>
";
}
连接建立正常,因为我可以访问在同一个脚本DB的其他部分。我怀疑我的查询有问题。
感谢
之前删除单引号'commentpost –
@ WebCode.ie ops thanks,fixed i吨。但看起来不是问题 –
**警告**:使用'mysqli'时,您应该使用[参数化查询](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php )和['bind_param'](http://php.net/manual/en/mysqli-stmt.bind-param.php)将用户数据添加到您的查询中。 **不要**使用字符串插值或连接来完成此操作,因为您创建了严重的[SQL注入漏洞](http://bobby-tables.com/)。 **不要**将'$ _POST','$ _GET'或**任何**用户数据直接放入查询中,如果有人试图利用您的错误,这可能会非常有害。 – tadman