gpt4 book ai didi

PHP、PDO bindValue - 如何传递逗号分隔的 INT 以用于 'in clause'

转载 作者:行者123 更新时间:2023-11-29 03:26:42 25 4
gpt4 key购买 nike

我正在尝试使用 PHP 和 PDO 获取与此类似的 MySQL 查询

select user_id from  users where user_id in (7,8,9)

如果我这样做:

$userlist ='7,8,9';
$stmt->bindValue(':userlist', $userlist, PDO::PARAM_STR);

MySQL服务器日志记录:

2016-01-27T16:51:52.644453Z   32 Prepare    select user_id from  users where user_id in (?)
2016-01-27T16:51:52.644489Z 32 Execute select user_id from users where user_id in ('7,8,9')

并且只返回user_id为7的行。

如果我这样做:

$userlist ='7,8,9';
$stmt->bindValue(':userlist', (int)$userlist, PDO::PARAM_INT);

MySQL 记录此:

2016-01-27T16:54:09.110990Z   33 Prepare    select user_id from  users where user_id in (?)
2016-01-27T16:54:09.111026Z 33 Execute select user_id from users where user_id in (7)

我又一次只看到了三行中的一行。我觉得这一定有一个非常基本的解决方案,但我一直找不到它..

最佳答案

您必须绑定(bind)每个值

select user_id from  users where user_id in (:id1,:id2,:id3)

对于这个查询:

$stmt->bindValue(':id1', 7);
$stmt->bindValue(':id2', 8);
$stmt->bindValue(':id3', 9);

或者您可以使用一些查询生成器。例如,您可以用 Yii2's query builder 做什么:

// ...WHERE (`status` = 10) AND (`type` IS NULL) AND (`id` IN (4, 8, 15))
$query->where([
'status' => 10,
'type' => null,
'id' => [4, 8, 15],
]);

关于PHP、PDO bindValue - 如何传递逗号分隔的 INT 以用于 'in clause',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35043491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com