Jump to content

Put a While statement into a variable


Bman900

Recommended Posts

<?php 
$result = mysql_query("SELECT email FROM newsletter ORDER BY id");


while($row = mysql_fetch_assoc( $result ))
{
    echo "".$row['email'].", ";

} 
?>

 

This prints a list like A@yahoo.com, b@yahoo.com, c@yahoo.com, which is perfect as I want to send out emails to those people but I need them in a variable such as $email to use with the mailer function. So how would I populate that list into a single variable?

Link to comment
Share on other sites

<?php 
$result = mysql_query("SELECT email FROM newsletter ORDER BY id");

$list = array();	// start the array
while($row = mysql_fetch_assoc( $result ))
{
    $list[] = $row['email'];	// put it into the list
echo "".$row['email'].", ";

}
// list the array
foreach($list as $email) {
echo $email.'<br>';
}
?>

Link to comment
Share on other sites

Ok am making this a bit more complicated. It worked as mentioned above but I want more. The emails won't send out when I add the following:

 

<?php
if($category == "All") {

$result = mysql_query("SELECT email FROM newsletter ORDER BY id");

}
else {

$result = mysql_query("SELECT email FROM newsletter WHERE category ='$category' ORDER BY id");

}
?>

 

Instead of this:

 

 

 

<?php

$result = mysql_query("SELECT email FROM newsletter ORDER BY id");

?>

 

Also here is my email code:

 


<?php

foreach($list as $email) {

      
        mail($email, $subject, $message,"From: $from_email\n" . "MIME-Version: 1.0\n" . "Content-type: text/html; charset=iso-8859-1"); 

       
        print "<br>$email\n";
       
        flush();
}

?>

 

 

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.