【发布时间】:2013-03-06 15:48:34
【问题描述】:
我正在尝试使用 php 的 PDO 对象从 mySQL 表中删除与一对条件匹配的所有行。我不知道为什么它不起作用:
//This deletes all comments for a given post;
//Querying database for existent comments on that post;
$this->query = $this->db->query(
"SELECT cid
FROM comments
WHERE id = '$html_data->id' AND pid = '$html_data->pid'"
);
//Fetching results into an array;
$this->rows = $this->query->fetchAll(PDO::FETCH_ASSOC);
//Deleting each comment on that post;
foreach ($this->rows as $this->row) {
$this->db->exec(
"DELETE from comments
WHERE cid = '$this->row['cid']'"
);
};
//Deleting the post itself;
$this->db->exec(
"DELETE from posts
WHERE id = '$html_data->id' AND pid = '$html_data->pid'"
);
//Deleting the post itself 有效,但 foreach 循环内的部分由于某种原因不起作用。为了调试,我在循环中添加了以下内容:
echo "WHERE cid = '{$this->row['cid']}'";
它按预期返回:
WHERE cid = '1'
WHERE cid = '2'
因此,获取的数据不是问题。我也试过了
WHERE id = '$html_data->id' AND pid = '$html_data->pid' AND cid = '$this->row['cid']'"
而不是只使用cid,它也不起作用。回声它返回,正如预期的那样:
WHERE id = '1' AND pid = '1' AND cid = '1'
WHERE id = '1' AND pid = '1' AND cid = '2'
是的,我检查了comments 表,我要删除的id、pid 和cid 与正在回显的那些匹配。
【问题讨论】:
-
只需
var_dump()查询,查找双引号或单引号,因为有时它们不存在或只是var_dump()查询,然后转到 phpmyadmin 并将查询粘贴到查询选项卡它会显示你有什么样的错误,还有DELETE FROM table_name WHERE id=1已经循环遍历表中的所有记录 -
因此,您的问题与 mysqli 无关,而与基本的 PHP 语法(以及 $this 关键字的不当使用)有关。另一个过于本地化的问题会污染网站
-
显然我不知道问题出在哪里,对我来说,在循环中使用 PDO/mySQL 是个问题,因为外面的代码几乎相同并且可以正常工作。不过下次我会考虑使用 Code Review 或其他网站,谢谢您的意见。
标签: php mysql pdo delete-row corresponding-records