PDA

View Full Version : extracting data for mail function


tsclan
08-25-2004, 02:01 PM
I have got a list of emails in mysql databse and want to use those emails in the php mail function. But for multiple emails you need a comma bettween the emails excpet for the last one where there is no comma at the end.

how can I extract the data so that it has a comma between the emails and no comma at the end of the list?

Oh and another thing what do i put in the MYSQL SELECT if I want only there emails if their "position" is either "admin" or "articles"
thanks

raf
08-25-2004, 02:51 PM
what MySQL version are you running?

If you have version 4.1 or higher, then you can use

select GROUP_CONCAT(email) as sendto, position from table GROUP BY position HAVING position In ('admin', 'articles')

this will return 2 records with a commadelimited list of email-values


If you have a lower version, then you best dumpt the emails in an array and then use an implode-function. this then needs to be done inside your serverside language (PHP ?)

like so

$sql = "select email from table where position In ('admin', 'articles')";
$result = mysql_query($sql) or die ('Queryproblem');
if (mysql_num_rows($result) >= 1){
while ($row=mysql_fetch_assoc($result)){
$arr_email[] = $row['email'];
}
$sendto = implode (', ', $arr_email);
//$sendto now contains a commadelimited list of email-values
}