我是MySQLi和PHP的新手,我试图通过匹配postid从我的数据库中选择评论。它应该选择具有该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>
";
}
连接建立正常,因为我可以在同一个脚本中访问数据库的其他部分。我怀疑我的查询存在问题。
由于
答案 0 :(得分:0)
使用tick来转义列而不是单引号:
SELECT *
FROM `articlecomments`
WHERE `commentpost` = 'post1'
LIMIT 30
答案 1 :(得分:0)
您必须使用$row['commentname']
和$row['commentvalue']
从数据库中获取数据。
echo "<script>alert('we have comments!');</script>
<div class='comment-single'>
<div class='row'>
<div class='col-md-3'>
" . $row['commentname'] . "
</div>
<div class='col-md-9'>
" . $row['commentvalue'] . "
</div>
</div>
</div>
";
答案 2 :(得分:0)
关于表格名称的单引号错误:
$get_comments = "SELECT * FROM 'articlecomments' WHERE 'commentpost' = 'post1' LIMIT 0 , 30";
正确的是:
$get_comments = "SELECT * FROM articlecomments WHERE commentpost = 'post1' LIMIT 0, 30";
必须写$ row:
" . $row['commentname'] . "
</div>
<div class='col-md-9'>
" . $row['commentvalue'] . "