mysql - PHP, PDO bindValue - how to pass comma seperated INTs for use with 'in clause' -
i'm trying mysql query similar 1 work using php , pdo
select user_id users user_id in (7,8,9)
if this:
$userlist ='7,8,9'; $stmt->bindvalue(':userlist', $userlist, pdo::param_str);
the mysql server log records:
2016-01-27t16:51:52.644453z 32 prepare select user_id users user_id in (?) 2016-01-27t16:51:52.644489z 32 execute select user_id users user_id in ('7,8,9')
and row user_id 7 returned.
if this:
$userlist ='7,8,9'; $stmt->bindvalue(':userlist', (int)$userlist, pdo::param_int);
mysql logs this:
2016-01-27t16:54:09.110990z 33 prepare select user_id users user_id in (?) 2016-01-27t16:54:09.111026z 33 execute select user_id users user_id in (7)
and again see 1 of 3 rows. feel must have basic solution, i've not been able find it..
you must bind each value
select user_id users user_id in (:id1,:id2,:id3)
for query:
$stmt->bindvalue(':id1', 7); $stmt->bindvalue(':id2', 8); $stmt->bindvalue(':id3', 9);
or may use query builder. example, may yii2's query builder:
// ...where (`status` = 10) , (`type` null) , (`id` in (4, 8, 15)) $query->where([ 'status' => 10, 'type' => null, 'id' => [4, 8, 15], ]);
Comments
Post a Comment