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
たいのは、エコーされているものと一致します。