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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.