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.