SQL bindParam 不工作
SQL bindParam not Working
我的代码有问题,我无法显示我的数据库中的数据我正在使用 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();
效果很好
提前致谢
在 bindValue()
代码中,您的循环是错误的。你应该遍历数组 $row
它应该是
foreach($row as $value){
echo "<td>". $value['content'] . "</td>";
}
我的代码有问题,我无法显示我的数据库中的数据我正在使用 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();
效果很好
提前致谢
在 bindValue()
代码中,您的循环是错误的。你应该遍历数组 $row
它应该是
foreach($row as $value){
echo "<td>". $value['content'] . "</td>";
}