Igor's Blog

PHP's PDO is a great way to access databases and works as expected on the most part, that is until you come to the IN() operator. Some peculiarities begin to emerge there as I've discovered recently when updating code for my travelblog.ws project.

Below is the kind of code I was running to execute a SQL statement and get back some results. Not showing the creation of the database connection ($dbConn) for brevity. The code is quite straight forward.
 PHP
$binds = ...; /* see specific examples below */
$sql = ...; /* see specific examples below */
$statement = $dbConn->prepare($sql);
$results = $statement->execute($binds);
$data = $statement->fetchAll(\PDO::FETCH_OBJ);


The IN() clause is actually very well documented in the PDOStatement API. It specifically states how it does not work:
Multiple values cannot be bound to a single parameter; for example, it is not allowed to bind two values to a single named parameter in an IN() clause.


So what does that mean? Well let's see. The following two bits of code produce the same results.
 PHP
$binds = array('id' => '0f841cb12dc75');
$sql = 'SELECT id FROM posts WHERE id = :id';


..and
 PHP
$binds = array('id' => '0f841cb12dc75');
$sql = 'SELECT id FROM posts WHERE id IN (:id)';


That's not surprising a single bind parameter for IN() and the equals (=) operator will produce identical results (print_r() output below).
pdoinbind3.png




When trying to combine multiple IDs into a single bind parameter for IN() such as both of the two examples below...
 PHP
$binds = array('id' => '0f841cb12dc75, 33a384024f785');
$sql = 'SELECT id FROM posts WHERE id IN (:id)';

 PHP
$binds = array('id' => "'0f841cb12dc75', '33a384024f785'");
$sql = 'SELECT id FROM posts WHERE id IN (:id)';


Gives no results. That's due to not being able to bind multiple values to a single bind parameter as in the PDOStatement documentation mentioned above.
pdoinbind1.png


However the following monstrosity works...
 PHP
$binds = array('id' => "'0f841cb12dc75', '33a384024f785'");
$sql = 'SELECT id FROM posts WHERE id IN (' . $binds['id'] . ')';


The above code of course doesn't use a bind parameter, just string concatenation to build up the SQL statement. Not surprisingly, both of the records are returned.
pdoinbind2.png



So what do you do if you want to have multiple bound values for the IN() operators? There are several options - either use the last approach by concatenating values into a string, which will expose you to SQL injection attacks and potential poor performance. You can use multiple bind parameters, one for each value you want to bind e.g. 'IN(:val1, :val2, val3)'. You could also convert the IN() to multiple OR clauses inside the SQL.

Which option is picked depends on the rest of your code. In my case I went for concatenation of the SQL string because it was the quickest to do without major refactoring and I didn't have to worry about SQL injection since all of my values were coming from other SQL query results first.

-i

comments powered by Disqus
Other posts you may like...
Programming, DIY, Games, Hacks, and Tech

Recent Blog Posts

Converting a CSV file containing commas in quotes to a pipe separated file using Awk and Bash

How to get Facybox gallery popup to work for images with dynamic URLs

jQuery UI Photoboxr plugin updated to version 0.2

Learn How to Add Disclaimer to All Outgoing Emails in Office 365

How to securely host your web site for free with Google Cloud and CloudFlare (part 3)

How to securely host your web site for free with Google Cloud and CloudFlare (part 2)

How to securely host your web site for free with Google Cloud and CloudFlare (part 1)

Beware of the subtle difference in date format strings between MySQL and SQLite

Postman reporting Unexpected 'N' error in response body

Adding extra shelves to the Ikea Detolf display cabinet

Recent Galleries

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

DIY spare parts computer build with a RAIDMAX Anura case

Fake 'Lepin' brand Lego packaging

Hardwood garden bench with clear resin void filler

Fixing a 3D printer extruder that stopped heating up

Easily increase disk space in a Lenovo Ideapad 100S 14" laptop with an M.2 SSD

Making a multi-piece 3D printed solder spool holder stand

DIY indoor apartment grow light wiring

Good Friday Electronics fun Easter Bunny LED PCB Kit IBEABU-01.0

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
300 George St Blogumentary

My Online Tools

UUID to OID Converter
Guru JSON-RPC Tester
Extrudifier Object Designer
Travel ┬ÁBlog

Blogs and Friends

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity
Please follow me for more great content!
     
Don't show this again