我正在尝试使用 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
and 它也没有工作。回声它返回,如预期的那样:
WHERE id = '1' AND pid = '1' AND cid = '1'
WHERE id = '1' AND pid = '1' AND cid = '2'
是的,我检查了comments
表格,id
并且pid
我cid
要删除的表格与正在回显的表格相匹配。