Re: PDO prepared statements and value list for MySQL "IN"

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



As you already know, you will dynamically create the $contents.

See if you can dynamically create the entire prepare statement which
includes the $contents, as well as dynamically create the bindValue
statement; then see if you can "eval" those dynamically created statements.

$commandPrepare = '$stmt = $pdo->prepare(\'
 select *
 from mytable
 where myfield IN ('.$dynamicallyCreatedContents.')\'
);';
eval($commandPrepare);

$commandBind = '$stmt->bindValue...  {escape quotes as needed -- I haven't
tested it}
eval($commandBind);
$stmt->execute();

On Tue, Jul 8, 2008 at 11:55 AM, TK <kmysql@xxxxxxxxxxxxxxx> wrote:

> I'd like to use a PDO prepared statement to perform a MySQL query that uses
> the IN function.
>
> I.e.:
> $stmt = $pdo->prepare('
>   select *
>   from mytable
>   where myfield IN (:contents)
> );
> $stmt->bindValue(':contents', $contents);
> $stmt->execute();
>
> Here's the problem:
>
> If $contents is set to a single value, everything's fine:
>   $contents = 'mystring';
>
> How can I include multiple values in here?  If $contents is set to an
> array, PHP throws an "Array to string conversion" notice.
> i.e. $contents = array('mystring1', 'mystring2');
>
> If $contents is set to a comma-separated list, no matches are returned
> (probably because the entire "list" is being passed to MySQL as a single
> value, not multiple values).
> I.e. $contents = 'mystring1,mystring2';
>
> What's the proper way to do this?  Can it be done?  (Note that I do not
> know how many elements will be in $contents ahead of time, so something like
> IN (:contents1, :contents2) wouldn't make sense.)
>
> Thanks for any help!
>
> - TK
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux