I have two tables named users and contacts. I would like to delete the records from two table at once. For that I am using the code below for now.
public function delete($userId, $contactId) {
/* Since Values cannot be mixed (the Numbers) with control flow logic
(the commas) with prepared statements needs one placeholder per Value. */
//As many question marks as array entries; the last one needs no comma
$questionMarks = str_repeat("?,", count($userId)-1)."?";
$sth = $this->dbh->prepare("DELETE FROM users WHERE id IN($questionMarks)");
$sth->execute($userId);
$questionMarks = str_repeat("?,", count($contactId)-1) . "?";
$sth = $this->dbh->prepare("DELETE FROM contacts WHERE id IN($questionMarks)");
$sth->execute($contactId);
}
Please note that $userId and $contactId will be an array and the count will always be equal.
How do I merge these two queries into one?
ANSWER :
The below query worked for me.
DELETE users,contacts FROM users INNER JOIN contacts WHERE users.id IN (2) AND contacts.id IN (2);
You need to find the way to join the tables together and use a query like