Home >Backend Development >PHP Tutorial >How to Delete Multiple Items with `WHERE... IN` in PDO Prepared Statements?
PDO Queries with "WHERE... IN" Clauses
When utilizing the "WHERE... IN" clause in PDO queries, developers often encounter issues resulting in only the deletion of the first item from the list. This arises due to the incompatibility of mixing values (numbers) with control flow logic (commas) within prepared statements. To resolve this, each value within the list requires its own placeholder.
Solution:
$idlist = array('260','201','221','216','217','169','210','212','213');
$questionmarks = str_repeat("?,", count($idlist)-1) . "?";
$stmt = $dbh->prepare("DELETE FROM `foo` WHERE `id` IN ($questionmarks)");
foreach ($idlist as $id) { $stmt->bindParam($i, $id); $i++; }
Once the statement is prepared and the parameters bound, executing the query will result in the successful deletion of all items specified in the $idlist array.
The above is the detailed content of How to Delete Multiple Items with `WHERE... IN` in PDO Prepared Statements?. For more information, please follow other related articles on the PHP Chinese website!