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?

//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 ,

//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);

 

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.