In your case it would be:
foreach($results as $row) {
$mail->AddAddress($row['email'], $row['first_name'].' '.$row['last_name']);
}
You need to refer the key values inside square brackets []
Pergunta
I'm trying to populate the email addresses in PHPMailer from an array returned by a query.
$query = 'SELECT u.id, u.first_name, u.last_name, u.email FROM users u INNER JOIN locations l ON (u.location_id = l.id) WHERE district_id = 8 GROUP BY id ORDER BY first_name, last_name ASC';
$result = mysqli_query($connection, $query);
if (!result) {
die("Database query failed: " . mysqli_error($result));
}
$recipients = array();
while ($row = mysqli_fetch_assoc($result)) {
$results[] = $row;
}
This gives me a result such as:
Array (
[0] => Array ( [id] => 1 [first_name] => Jon [last_name] => Smith [email] => jon@domain.com )
[1] => Array ( [id] => 3 [first_name] => Dave [last_name] => Virk [email] => dave@domain.com )
[2] => Array ( [id] => 2 [first_name] => Chris [last_name] => West [email] => chris@domain.com )
)
But being new to working with arrays like this I'm not sure how to extract the name and email of each user for PHPMailer.
I read an example that did something like this:
foreach($recipients as $email => $name) {
$mail->AddAddress($email, $name);
}
How would I adapt this?
Solução
In your case it would be:
foreach($results as $row) {
$mail->AddAddress($row['email'], $row['first_name'].' '.$row['last_name']);
}
You need to refer the key values inside square brackets []