Php SQL bindParam不工作
我的代码有问题,我无法显示数据库中的数据。我正在使用bindParam()和bindValue(),它仍然不起作用 这是我的代码:Php SQL bindParam不工作,php,mysql,sql,pdo,bindparam,Php,Mysql,Sql,Pdo,Bindparam,我的代码有问题,我无法显示数据库中的数据。我正在使用bindParam()和bindValue(),它仍然不起作用 这是我的代码: $id = $_GET['id']; try{ $database = new Connection(); $db = $database->openConnection(); $sql = "SELECT users.username, users.user_id, topic.*, post.* FROM
$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();
}
但是当我被利用的时候
$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();
它工作得很好
提前感谢在bindValue()
代码中,您的循环是错误的。您应该在数组上循环$row
它应该是
foreach($row as $value){
echo "<td>". $value['content'] . "</td>";
}
foreach($row作为$value){
回显“$value['content']”;
}
$db->query($sql)
您正在重新运行查询,而不是获取结果。您的$id变量是否包含“:id”?例子:
foreach($row as $value){
echo "<td>". $value['content'] . "</td>";
}