1

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 Answers 2

5
$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.

Sign up to request clarification or add additional context in comments.

Comments

2

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})";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.