Let's say there 'sa 30- question in my hand. I would like to delete these 30 questions at a time. But there are some coding errors and much appreciated if you could help me
public function Clear()
{
$stmt = $this->db->prepare("SELECT id FROM ask_data WHERE standart=0");
$stmt->execute();
if ($stmt->rowCount() > 0)
{
$ask_cnt = $stmt->rowCount();
$ask_data = array();
while ($row = $stmt->fetch())
{
$data = array("ask_id" => $row['id']);
$stmt2 = $this->db->prepare("DELETE FROM id FROM questions WHERE ask_id=(:ask_id)");
$stmt->bindParam(':ask_id', $ask_data[$i], PDO::PARAM_INT);
$stmt2->execute();
}
$i = 0;
$count = count($ask_id);
for ($i=0;$i<$count;$i++)
{
$data = array("ask_id" => $row['id']);
$stmt2 = $this->db->prepare("DELETE FROM id FROM questions WHERE ask_id=(:ask_id)");
$stmt->bindParam(':ask_id', $ask_data[$i], PDO::PARAM_INT);
$stmt2->execute();
}
}
}
The delete
statement is wrong - you have two from
clauses. You should remove the FROM id
:
DELETE FROM questions WHERE ask_id=(:ask_id)
Having said that, you could dramatically simplify your program by using the IN
operator instead of a separate query:
DELETE FROM questions
WHERE ask_id IN (SELECT id
FROM ask_data
WHERE standart = 0)
hy,Your delete query is not correct.You can use it like
"DELETE FROM questions WHERE ask_id=".$row['id'].";
I'm not understanding why you need a loops in your code to run thirty separate DELETE statement.
If the goal is delete rows from the questions
table which have an ask_id
column value that matches an id
value from ask_data
where the standart
column has a value of zero...
this could be done in one SQL statement. One database roundtrip. No need for a separate SELECT statement, fetching rows, and running through a loop to perform more SQL statements.
DELETE q.*
FROM ask_data d
JOIN questions q
ON q.ask_id = d.id
WHERE d.standart=0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.