php pdo mysql where in,PHP PDO:数组中更新SQL WHERE IN()子句

I'm trying to take an array of ID numbers and update every row with that ID number. PHP PDO code follows:

private function markAsDelivered($ids) {

$update = $this->dbh->prepare("

UPDATE notifications

SET notified = 1

WHERE notification_id IN (

:ids

)

");

$ids = join(',', $ids);

Logger::log("Marking the following as delivered: " . $ids, $this->dbh);

$update->bindParam(":ids", $ids, PDO::PARAM_STR);

$update->execute();

}

However, when this is run, only the first item in the list is getting updated, although multiple ID numbers are being logged. How do I modify this to update more than one row?

解决方案

A placeholder can only represent a single, atomic value. The reason it kinda works is because the value mysql sees is of the form '123,456' which it interprets as an integer, but discards the rest of the string once it encounters the non numeric part(the comma).

Instead, do something like

$list = join(',', array_fill(0, count($ids), '?'));

echo $sql = "...where notification_id IN ($list)";

$this->dbh->prepare($sql)->execute(array_values($ids));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值