364,085 Members | 5234 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Pass a PHP array into Mysql 'IN' clause

bilibytes
100+
P: 127
Hi there,

is it possible to pass a PHP array to get some data matching in a Mysql Table?

lets say i have a list of names that i get from PHP into an array:

Expand|Select|Wrap|Line Numbers
  1. $php_array = array(0=>'john', 1=>'marcus', 2=>'robert', 3=>'samuel'...)
and i want to know which of these are in my Table.

could i do:

Expand|Select|Wrap|Line Numbers
  1. SELECT user_id
  2. FROM users
  3. WHERE user_name IN ($php_array)
i don't know if mysql can make it..

is there a way to perform this?


thank you,

best regards
Nov 14 '08 #1
Share this Question
Share on Google+
2 Replies


Atli
Expert Mod 2.5K+
P: 4,459
Hi.

You could try the implode function.

For example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $array = array("first", "second", "third");
  3. $list = "'". implode("', '", $array) ."'";
  4. echo $list; // Outputs: 'first', 'second', 'third'
  5. ?>
  6.  
The $list string could then be used in the IN function.
Nov 14 '08 #2

bilibytes
100+
P: 127
ok great, thankyou very much!

bilibytes
Nov 17 '08 #3

Post your reply

Help answer this question



Didn't find the answer to your MySQL Database question?

You can also browse similar questions: MySQL Database