Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

given an array

$galleries = array
         (
           [0] => 1
           [1] => 2
           [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?

share|improve this question

7 Answers

up vote 137 down vote accepted
$ids = join(',',$galleries);  
$sql = "SELECT * FROM galleries WHERE id IN ($ids)";
share|improve this answer
1  
I think this technically won't work, since the entire string is encapsulated in double quotes. – AvatarKava May 25 '09 at 19:39
Agreed. Fixed the code. – Flavius Stef May 25 '09 at 19:41
1  
The identifiers are still a quoted list, so it comes out as "WHERE id IN ( '1,2,3,4' )", for example. You need to quote each identifier separately, or else ditch the quotes inside the parentheses. – Rob May 25 '09 at 20:05
15  
Actually, you can do.. $ids = join("','", $galleries); $sql = "SELECT * FROM galleries WHERE id IN ('$ids')"; – MiffTheFox May 25 '09 at 20:41
4  
I just add the warning that $galleries should be input validated before this statement! Prepared statements can't handle arrays AFAIK, so if you are used to bound variables, you could easily make SQL injection possible here. – leemes Mar 27 '12 at 11:07

ints:

$query = "SELECT * FROM `$table` WHERE `$column` IN(".implode(',',$array).')';

strings:

$query = "SELECT * FROM `$table` WHERE `$column` IN('".implode("','",$array).'\')';
share|improve this answer

Assuming you properly sanitize your inputs beforehand...

$matches = implode(',', $galleries);

Then just adjust your query:

SELECT *
FROM galleries
WHERE id IN ( $matches )

Quote values appropriately depending on your dataset.

share|improve this answer
Yea, yours is better than mine was :P – Aiden Bell May 25 '09 at 19:39
select id from galleries where id in (1, 2, 5);

Simple for each loop will work.

EDIT: Flavius/AvatarKava's way is better, but make sure that none of the array values contain commas.

share|improve this answer

Because the original question relates to an array of numbers and I am using an array of strings I couldnt make the given examples work.

I found that each string needed to be encapsulated in single quotes to work with the IN() function.

Here is my solution

foreach($status as $status_a) {
        $status_sql[] = '\''.$status_a.'\'';
    }
    $status = implode(',',$status_sql);

$sql = mysql_query("SELECT * FROM table WHERE id IN ($status)");

As you can see the first function wraps each array variable in single quotes (\') and then implodes the array.

NOTE: $status does not have single quotes in the SQL statement.

There is probably a nicer way to add the quotes but this works.

share|improve this answer

You may have table texts (T_ID (int), T_TEXT (text)) and table test (id (int), var (varchar(255)))

in insert into test values (1, '1,2,3') ;

the following will output rows from table texts where T_ID IN (1,2,3)

SELECT * FROM `texts` WHERE (SELECT FIND_IN_SET( T_ID, ( SELECT var FROM test WHERE id =1 ) ) AS tm) >0

this way you can manage a simple n2m db relation without extra table and using only SQL without need to us PHP or other programming lang.

share|improve this answer
$ids = "(".implode(',',$galleries).")";  // you can remplace "implode" with "join"  
$sql = "SELECT * FROM galleries WHERE id IN $ids";
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.