我的代码有问题,我无法从我的数据库中显示数据--我使用的是bindParam()和bindValue() --它仍然不能工作。
这是我的密码:
$id = $_GET['id'];
try{
$database = new Connection();
$db = $database->openConnection();
$sql = "SELECT users.username, users.user_id, topic.*, post.*
FROM users INNER JOIN post
ON users.user_id = post.user_id
INNER JOIN topic
ON topic.topic_id = post.topic_id
WHERE topic.topic_id = :id";
$stm = $db->prepare($sql);
$stm->bindValue(":id", $id);
$stm->execute();
$row = $stm->fetch();
foreach($db->query($sql) as $row){
echo "<td>". $row['content'] . "</td>";
}
}catch(PDOException $e){
echo 'Connection Problem: '. $e->getMessage();
}我明白这个错误:
Connection Problem: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':id' at line 6 但当我被利用时
$sql = "SELECT users.username, users.user_id, topic.*, post.*
FROM users INNER JOIN post
ON users.user_id = post.user_id
INNER JOIN topic
ON topic.topic_id = post.topic_id
WHERE topic.topic_id = ". $id;
$stm = $db->prepare($sql);
$stm->execute();它工作得很完美
提前感谢
发布于 2018-04-05 15:49:40
在bindValue()代码中,您的循环是错误的。您应该在数组$row上循环它应该是
foreach($row as $value){
echo "<td>". $value['content'] . "</td>";
}https://stackoverflow.com/questions/49676410
复制相似问题