Jump to content

Getting a list of email addresses from a database


jakeoh

Recommended Posts

Noob here, obviously.

 

I have a database of users, each with an email address. I would like to get a string with every address followed by a comma.

 

My SQL query will go "SELECT email FROM users", but how do I format the results so that I have a nice string with all addresses separated with a comma?

Link to comment
Share on other sites

//assume DB conn made already
$q = mysql_query('SELECT email FROM users');
$emails = array();
while($r = mysql_query($q)) {
    $emails[] = $r['email'];
}
$commaSeperated = implode(',', $emails);

//or, a way to do it without array/implode (might be faster)
$q = mysql_query('SELECT email FROM users');
$emails = '';
while($r = mysql_query($q)) {
    $emails .= $r['email'] . ',';
}
$emails = substr($emails, 0, strlen($emails)-2); //get rid of the trailing ,

Link to comment
Share on other sites

//assume DB conn made already
$q = mysql_query('SELECT email FROM users');
$emails = array();
while($r = mysql_query($q)) {
     $emails[] = $r['email'];
}
$commaSeperated = implode(',', $emails);

//or, a way to do it without array/implode (might be faster)
$q = mysql_query('SELECT email FROM users');
$emails = '';
while($r = mysql_query($q)) {
     $emails .= $r['email'] . ',';
}
$emails = substr($emails, 0, strlen($emails)-2); //get rid of the trailing ,

 

you will end up having comma at the end of string think about that

 

maybe .. is what youre trying to do

 

while($r = mysql_query($q)) {

    $emails[]= $r['email'];

}

$emails = implode(',',$emails);

 

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.