PDO Statement with "IN" Clause

Hello friends,

I am struggling with the following issue.

I want to select some data from a table:


SELECT * FROM `user` WHERE `email` IN :email

My Problem is how to prepare the data I want to bind to the :email parameter?

I wrote myself a function which takes all input (i.e. "me@example.com, you@example.com, hesheit@example.com") und returns a string like “‘me@example.com’,‘you@example.com’,‘hesheit@example.com’” (without the double quotes) but when i use the following code:


$query->bindValue(':email',$email,PDO::PARAM_STR);

PDO wraps it up with double quotes and the query returns "nothing" as a result.

What am I doing wrong?

Best regards,

dj pogo

did u want select like


$emails = array(

   'a@me.com',

   'b@me.com',

   'c@me.com',

);


$sql = 'select * from users where email in (:emails)';

$users = $db->createCommand($sql)

            ->bindValue(':emails', implode(',', $emails))

            ->queryAll();



if right, i think u should give up and try another way to fetch. becuz pdo will convert your sql like

[sql]select * from users where email in ("a@me.com, b@me.com, c@me.com")[/sql].

Quotation marks will be added by PDO class if u using bindValue or bindParams. and if u find here http://www.php.net/manual/en/pdo.constants.php and u will relized that there’s no PDO::PARAM_ARRAY~

I suggest that you can using CDbCriteria::addInCondition to solve this problem. if you like using DAO directly, you can check the sources of that class and see how it achieved.