Jump to content

query: SELECT email FROM table WHERE ID = 'any from array'


RLJ

Recommended Posts

Hi all, probably a fairly straightforward question, but I can't find the answer I'm looking for:

 

I have an array 'idents' that contains ID numbers. I now want to write a MySQL query that selects all values of the table-field 'email' where the field 'ID' = any of the IDs stored in 'idents'. Then I want to put those emails in an array 'emails'.

 

So e.g. my table is as follows:

ID        |  email

----------------------

111111 |  email1

222222 |  email2

333333 |  email3

444444 |  email4

555555 |  email5

666666 |  email6

 

and the array 'idents' is as follows: '111111','444444','555555'

 

then I want the query to return 'emails' = array('email1','email4','email5');

 

Thanks for your help!

SELECT FROM . . .  WHERE . . . IN()

 

Implode the array into a string with "', '" as the glue, then use that string in the IN()

 

$string = implode("', '", $idents);
$query = "SELECT `email` FROM `table` WHERE `id` IN( '" . $string . "')";

 

Also, since this ends up being more of a php question, moving thread to php coding help . . .

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.