tags:

views:

73

answers:

2

I have an array of employee ids:

34 , 35, 40, 67, 54 and so on.

What would be the simplest way to query my 'employees' table and find all the names of the correlated employees?

That is a query that would return the 'name' for each of the ids in the array.

+2  A: 

Implode your array into a string, and then use the IN() function of MySQL.

SELECT col1, col2, col3
FROM employees
WHERE col1 IN(1, 12, 38, 52)

You can get that in string with implode()

$ids = array(1,132,32,52);
$inString = implode(",",$ids);

$query = "SELECT col1, col2, col3
          FROM employees
          WHERE col1 IN ({$inString})";
Jonathan Sampson
+5  A: 
$id_str = implode(', ', $ids);
mysql_query("SELECT name FROM employees WHERE id IN ($id_str)");

If you want them all in one result row, use mysql's GROUP CONCAT.

code_burgar