我正在尝试删除一个帖子,然后选择并显示另一个带有 PDO 的帖子。我想用一种方法做到这一点:
public function remove_and_renew_item(){
$this->query = $this->conn->prepare('DELETE FROM posts WHERE post_id=:remove_id');
$this->query = $this->conn->prepare('SELECT * FROM posts WHERE post_id=:next_id');
$this->query->bindParam(':remove_id', $this->remove_id);
$this->query->bindParam(':next_id', $this->next_id);
$this->query->execute();
echo json_encode($this->query->fetch(PDO::FETCH_ASSOC));
}
但是,我的AJAX报错了。有没有问题是因为:
- 我有两个不同的查询?
- 它是否也在尝试获取查询的删除部分?
如果我删除单个查询,并且它与 bindParam 相关,它就可以工作。然而,他们没有在一起。谁能弄清楚为什么他们不一起工作?
最佳答案
您正在用第二个查询覆盖第一个查询。您想做的事并非不可能,但您做事的方式不对。
$this->query1 = $this->conn->prepare('DELETE FROM posts WHERE post_id=:remove_id');
$this->query2 = $this->conn->prepare('SELECT * FROM posts WHERE post_id=:next_id');
关于php - 在同一个 PDO 查询中删除和选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23895231/