How does PDO bind the array variable of the IN () statement?

Source: Internet
Author: User
My SQL statement contains the IN condition. IN my imagination, I can write code {code...} like this ...} however, if pdo is executed in this way, an error will be reported, and an array variable cannot be bound. Is there any good solution? In My SQL statements INIn my imagination, I can write code like this.

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

However, if pdo is executed in this way, an error will be reported and an array variable cannot be bound. Is there a good solution?

Reply content:

In My SQL statementsINIn my imagination, I can write code like this.

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

However, if pdo is executed in this way, an error will be reported and an array variable cannot be bound. Is there a good solution?

PDO does not support array binding.
Or do not bind it,

$ Ids = array (2344,552 3, 9332); // filter ids slightly $ in = implode (',', $ ids ); $ st = $ pdo-> prepare ('select * FROM table_name WHERE id IN ('. $ in. '); $ st-> execute ();

If you insist on binding, you may have

$ Ids = array (2344,552 3, 9332); // multiple? Slightly $ st = $ pdo-> prepare ('select * FROM table_name WHERE id IN (?,?,?) '); Foreach ($ ids as $ k => $ id) $ st-> bindValue ($ k + 1), $ id); $ st-> execute ();

This problem was just mentioned in the morning. PDO does not support such binding. For details, refer:
Http://segmentfault.com/q/10100000001... article 3.

It was specified during lotusphp writing in 08 years. PDO does not support this. zhao yi upstairs is also a lotusphp developer.

PDO does not support such binding, but it can be implemented through a third-party Class. This is a PDO Class I have developed, which can implement secure WHERE IN and is convenient to use.

Https://github.com/lincanbin/PHP-PDO-MySQL-Class
It's easy to use. First, create an object.


  

Then


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

You can see the https://github.com/lincanbin/PHP-PDO-MySQL-Class with other SQL syntax.

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.