Given an array of ids $galleries = array(1,2,5)
I want to have a SQL query that uses the values of the array in its WHERE clause like:
SELECT *
FROM galleries
WHERE id = /* values of array $galleries... eg. (1 || 2 || 5) */
How can I generate this query string to use with MySQL?
We should take care of SQL injection vulnerabilities and an empty condition. I am going to handle both as below.
For a pure numeric array, use the appropriate type conversion viz
intval
orfloatval
ordoubleval
over each element. For string typesmysqli_real_escape_string()
which may also be applied to numeric values if you wish. MySQL allows numbers as well as date variants as string.To appropriately escape the values before passing to the query, create a function similar to:
Such a function would most likely be already available to you in your application, or maybe you've already created one.
Sanitize the string array like:
A numeric array can be sanitized using
intval
orfloatval
ordoubleval
instead as suitable:Then finally build the query condition
or
Since the array can also be empty sometimes, like
$galleries = array();
we should therefore note thatIN ()
does not allow for an empty list. One can also useOR
instead, but the problem remains. So the above check,count($values)
, is to ensure the same.And add it to the final query:
Col. Shrapnel's SafeMySQL library for PHP provides type-hinted placeholders in its parametrised queries, and includes a couple of convenient placeholders for working with arrays. The
?a
placeholder expands out an array to a comma-separated list of escaped strings*.For example:
* Note that since MySQL performs automatic type coercion, it doesn't matter that SafeMySQL will convert the ids above to strings - you'll still get the correct result.
Using PDO:[1]
Using MySQLi [2]
Explanation:
Use the SQL
IN()
operator to check if a value exists in a given list.In general it looks like this:
We can build an expression to place inside the
()
from our array. Note that there must be at least one value inside the parenthesis or MySQL will return an error; this equates to making sure that our input array has at least one value. To help prevent against SQL injection attacks, first generate a?
for each input item to create a parameterized query. Here I assume that the array containing your ids is called$ids
:Given an input array of three items
$select
will look like:Again note that there is a
?
for each item in the input array. Then we'll use PDO or MySQLi to prepare and execute the query as noted above.Using the
IN()
operator with stringsIt is easy to change between strings and integers because of the bound parameters. For PDO there is no change required; for MySQLi change
str_repeat('i',
tostr_repeat('s',
if you need to check strings.[1]: I've omitted some error checking for brevity. You need to check for the usual errors for each database method (or set your DB driver to throw exceptions).
[2]: Requires PHP 5.6 or higher. Again I've omitted some error checking for brevity.
More an example:
Use:
A simple
for each
loop will work.Flavius/AvatarKava's way is better, but make sure that none of the array values contain commas.