How PDO binds the array variable of the in () statement

Source: Internet
Author: User
My SQL statement has the INThe conditions, in my imagination can write code like this

$ids = Array (2344, 5523, 9332), $st = $pdo->prepare (' SELECT * FROM table_name WHERE ID (: ID) '); $st->bindparam (' id ') , $ids); $st->execute ();

But this implementation PDO will error, unable to bind an array variable, there is no good solution

Reply content:

I have a condition in my SQL statement that IN can be written like this in my imagination

$ids = Array (2344, 5523, 9332), $st = $pdo->prepare (' SELECT * FROM table_name WHERE ID (: ID) '); $st->bindparam (' id ') , $ids); $st->execute ();

But this implementation PDO will error, unable to bind an array variable, there is no good solution

PDO does not support binding arrays.
Or you don't have to use bindings.

$ids = Array (2344, 5523, 9332);//filter ids slightly $in = implode (', ', $ids); $st = $pdo->prepare (' SELECT * ' from table_name WHERE ID in ('. $in. ') '); $st->execute ();

If you stick with the bindings, maybe that's it.

$ids = Array (2344, 5523, 9332);//automatic construction of multiple $st = $pdo->prepare (' SELECT * FROM table_name WHERE ID in (?,?,?) '); foreach ($ids as $k = $id)    $st->bindvalue (($k + 1), $id); $st->execute ();

I just said this in the morning. PDO does not support this binding, see:
http://segmentfault.com/q/10100000001 ... Third article.

08 wrote Lotusphp's documentation specifically annotated, PDO does not support this, Upstairs Zhao Yi is also lotusphp developers

PDO does not support this binding, but it can be done by a third-party class, which is a PDO class that I do, which makes it easy to use where in.

Https://github.com/lincanbin/PHP-PDO-MySQL-Class
The use of the method is also very simple, first a new object


  
   

And then


  
   query("SELECT * FROM fruit WHERE name IN (?)",array('apple','banana'));?>

, other SQL syntax can be used to see the Https://github.com/lincanbin/PHP-PDO-MySQL-Class

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.