datafan Posted August 27, 2007 Share Posted August 27, 2007 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; } Quote Link to comment https://forums.phpfreaks.com/topic/66928-solved-convert-mysql_fetch_array-to-string-variable/ Share on other sites More sharing options...
sasa Posted August 27, 2007 Share Posted August 27, 2007 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; Quote Link to comment https://forums.phpfreaks.com/topic/66928-solved-convert-mysql_fetch_array-to-string-variable/#findComment-335557 Share on other sites More sharing options...
Wuhtzu Posted August 27, 2007 Share Posted August 27, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/66928-solved-convert-mysql_fetch_array-to-string-variable/#findComment-335564 Share on other sites More sharing options...
Wuhtzu Posted August 27, 2007 Share Posted August 27, 2007 The simple and most useful method is sasa's. I misunderstood the post a bit Quote Link to comment https://forums.phpfreaks.com/topic/66928-solved-convert-mysql_fetch_array-to-string-variable/#findComment-335565 Share on other sites More sharing options...
datafan Posted August 27, 2007 Author Share Posted August 27, 2007 Excellent! Thank you both, it's always so simple when someone who knows what their doing does it. lol Quote Link to comment https://forums.phpfreaks.com/topic/66928-solved-convert-mysql_fetch_array-to-string-variable/#findComment-335685 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.