Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DELETE multiple rows in PDO

I'm a rookie in PDO and I've done some search about the issue I'm facing and I wasn't able to find any answers about it. As you can see below, I have this function:

function deleteInfo($id){
    $pdo = connPDO();
    $deleteInfo = $pdo -> prepare("DELETE FROM game_locais_zumbis WHERE id_zumbi IN (:id)");
    $deleteInfo -> bindValue(":id", $id, PDO::PARAM_STR);
    $deleteInfo -> execute();
    $pdo = null;
}

After that, I have the following code:

while($row = $listInfo -> fetch(PDO::FETCH_ASSOC)){
    $ids[] = $row['ids'];
}
$ids = implode(',', $ids);
deleteInfo($ids);

When I echo my $ids, I get:

1,2,3,4,5

But the DELETE function is not deleting all those five rows in my db but only the first one, like "1". When I run that exactly same DELETE function in my db, replacing the ":id" with "1,2,3,4,5", it does work! Does anybody know what's my mistake here? I appreciate any help.

like image 203
user3854140 Avatar asked Jan 10 '23 00:01

user3854140


1 Answers

I would do this:

$query = "DELETE FROM game_locais_zumbis WHERE id_zumbi in (".str_repeat("?,", count($ids) - 1)."?)";
$stmt = $conn->prepare($query);
$stmt->execute($ids);
like image 51
Shawn Avatar answered Jan 23 '23 00:01

Shawn