Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

How to properly bind array param in raw query with WHERE IN/WHERE NOT IN

Hello,

How can I properly bind an array for a raw query's NOT IN / IN?

The following throws an array to string conversion error:

$user_list_to_exclude = [1, 2, 3];
    
$data = DB::select("SELECT * FROM users WHERE user_id NOT IN :user_list_to_exclude",
     ['user_list_to_exclude' => $user_list_to_exclude]);

Thanks

0 likes
2 replies
Sergiu17's avatar

something like this

DB::select("SELECT * FROM users WHERE user_id NOT IN (:user_list_to_exclude)", [
    'user_list_to_exclude' => implode(', ', $user_list_to_exclude)
]);
1 like
Ligonsker's avatar

@Sergiu17 it didn't work either, in my case, i get an error that the column in the table is an integer and it fails to convert nvarchar to int.

Please or to participate in this conversation.