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.
The IN() clause is actually very well documented in the PDOStatement API. It specifically states how it does not work:
So what does that mean? Well let's see. The following two bits of code produce the same results.
That's not surprising a single bind parameter for IN() and the equals (=) operator will produce identical results (print_r() output below).
When trying to combine multiple IDs into a single bind parameter for IN() such as both of the two examples below...
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.
However the following monstrosity works...
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.
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.