2

I am trying to figure out how to put a mysql query result into an array that can be accessed outside the while loop. Is this possible?

My test code that I am playing with is below. I want to send email to the email addresses in the array without creating the email code inside the while loop. That way I can inject the array result into the BCC field and it all goes out at once using the mail() function rather than opening a new smtp connection for each email - or so I think.

<?php  
if(isset($_POST['btnSendToSelected']) && isset($_POST['checked']))
{
    $checked = array_map('intval',$_POST['checked']);
    $email_list = implode(", ", $checked);

    $get_emails = mysqli_query($conn, "SELECT UserName, Email FROM users WHERE UserId IN ($email_list)")
    or die($dataaccess_error);

    while($row = mysqli_fetch_array($get_emails))
    {
        $emails = array($row['Email']);
        $emails_array = implode(", ", $emails);
    }
        // send the email here outside the while loop...
}
elseif(isset($_POST['btnSendToSelected']) && !isset($_POST['checked']))
{
    $msg = $msg_error;
}
?>

3 Answers 3

4
$emails_array = implode(", ", $emails);

You can explode this line wherever you want to get your array.

//EDIT

It looks like that whole while loop can be handled a bit better. I suggest

$emails = array();
while($row = mysqli_fetch_array($get_emails))
    {
        $emails[] = $row['Email']."|||".$row['Username'];
    }

Then you've got all the info in the $emails array in one go.

The ||| is just a random delimiter I chose, You could use a comma or something. Alternatively, you could make that entry a 2-d array, something like

$emails[][0] = $row['Email'];
$emails[][1] = $row['Username'];

...although that specific code probably won't work, the idea will. Then, just access it like this:

echo "Username 99 is ".$emails[99][1];
Sign up to request clarification or add additional context in comments.

2 Comments

Was about to post the exact same thing with one addendum - after the loop, you can create the list of email addresses for the BCC field using $emailList = implode(', ', $emails);
Thanks for taking the high road and not duplicating my post. That's been happening (to me) a lot lately so I really appreciate the chance to be +1ed. :)
2
$emails_array = array();

while($row = mysqli_fetch_array($get_emails)) {
    $emails_array[] = explode(", ", $row['Email']);
}

foreach($emails_array as $value) {
  mail_function($value);
}

Or

while($row = mysqli_fetch_array($get_emails)) {
  foreach(explode(", ", $row['Email']) as $value) {
    mail_function($value);
  }
}

2 Comments

This solution answers the question as asked. I'm still not sure it is the correct solution for the end goal however...
Added a nested version for completeness.
1

The email is still stored in the variable $row. And $email... You can also do this:

while($row = mysqli_fetch_array($get_emails))
{
    mail($row['Email'],$subject,$message,$headers);
}

If you want to store the emails in an array:

$emails = array();
while($row = mysqli_fetch_array($get_emails))
{
    $emails[] = $row['Email'];
}

3 Comments

but when I echo out the array, it only shows a single email address. if I echo it from inside the loop, I get them all.
Check out that edit... this populates an array with the emails. You can access them by looping through the array or calling them by their numeric array key.
yeah, none of these seems to work for me. sorry I'm not sure if I'm doing it right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.