【发布时间】:2017-03-23 23:21:47
【问题描述】:
我是 MySQLi 和 PHP 的新手,我正在尝试通过匹配 postid 从我的数据库中选择 cmets。它应该选择所有具有该 ID 的行,并显示每个行的名称和值。但不工作。
// 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>
";
}
连接建立得很好,因为我可以在同一个脚本中访问数据库的其他部分。我怀疑我的查询有问题。
谢谢
【问题讨论】:
-
去掉'commentpost前的单引号
-
@WebCode.ie ops 谢谢,已修复。然而,这似乎不是问题
-
警告:使用
mysqli时,您应该使用parameterized queries 和bind_param将用户数据添加到您的查询中。 请勿使用字符串插值或连接来完成此操作,因为您创建了一个严重的SQL injection bug。 切勿将$_POST、$_GET或任何用户数据直接放入查询中,如果有人试图利用您的错误,这可能会非常有害。