Jump to content

[SOLVED] convert mysql_fetch_array to string variable?


datafan

Recommended Posts

I can't seem to get this, I am grabbing a list of email addresses from my database. I want to then convert the email addresses to a variable string that I can put in the "to" field of my email. Why is this so hard to do?

I can get it to output the emails on the screen with a comma between them but how do I make that string a variable that I can use in my email script later (outside the "while" loop)?  thanks...

 

 


$getmail = mysql_query("SELECT email FROM users", GetMyConnection() )or die(mysql_error());

while ($allmail = mysql_fetch_array($getmail)){
echo $allmail['email']. ",";
//$to = THIS IS WHERE I NEED HELP;
}

 

 

Link to comment
Share on other sites

try

$getmail = mysql_query("SELECT email FROM users", GetMyConnection() )or die(mysql_error());

while ($allmail = mysql_fetch_array($getmail)){
$to[] = $allmail['email'];
//$to = THIS IS WHERE I NEED HELP;
}
$to = implode(', ',$to);
echo $to;

Link to comment
Share on other sites

This is the short answer... you simply state that $to holds the same value as $allmail['email'] ($to = $allmail['email']):

<?php
$getmail = mysql_query("SELECT email FROM users", GetMyConnection() )or die(mysql_error());

while ($allmail = mysql_fetch_array($getmail)){
    echo $allmail['email']. ",";
    $to = $allmail['email'];

}
?>

 

But of course this would overwrite your variable $to for each loop in your while statement. So you have two options:

 

#1: Send a mail in each loop:

<?php
while ($allmail = mysql_fetch_array($getmail)){
    $to = $allmail['email'];

    mail($to, $subject, $message);
}
?>

 

 

#2: Create a list of all the email addresses separated by comma and then send a mail with multiple receptionists

<?php
while ($allmail = mysql_fetch_array($getmail)){
    //Add an email address followed by a comma and a space for each loop
    $to . = $allmail['email'] . ', ';

    //Remove the last comma and space
    $to = substr($to,0,strlen($to)-2);

}

//Send the mail with multiple receptionists
mail($to, $subject, $message);
?>

 

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.