Inability to Bind Parameters for WHERE IN Clause with PDO
Your code attempts to bind a parameter for the WHERE IN clause using PDO, but it consistently returns a count of 1. Replacing the parameterized value with the variable itself yields an accurate count. This discrepancy arises because PDO cannot bind parameters for IN clauses effectively.
The provided array is imploded into a comma-separated string and assigned as the parameter value. However, the database interprets this string as a single value, similar to using:
SELECT foo FROM bar WHERE ids IN ('1,2,3')
Despite the presence of multiple values, the database treats it as a single string. To resolve this issue, you must manually insert the IN list into the query:
'SELECT foo FROM bar WHERE ids IN (' . $myArray .')'
This method ensures that each array element is properly included in the IN clause. Unfortunately, there is currently no other viable solution for binding parameters within WHERE IN clauses using PDO.
The above is the detailed content of Why Does PDO Fail to Bind Parameters in WHERE IN Clauses?. For more information, please follow other related articles on the PHP Chinese website!