4
I’m trying to send a newsletter after a search in my database, it’s the first time I’m using the Mysqli extension and I’m having some difficulty going through the second WHILE and I believe it’s not the right way, I did so:
$mysqli = new mysqli($host,$user, $password, $database); $sql = "SELECT id, email FROM newsletter WHERE status = 1 AND enviado = 0"; $result = $mysqli->query($sql) or trigger_error($mysqli->error." [$sql]"); while($row = $result->fetch_array()) { $id = $row['id']; $email = $row['email']; } while($row = $result->fetch_assoc()) { $mail->setFrom('[email protected]', 'Newsletter'); $mail->addAddress($email); $mail->Subject = 'Envio Newsletter'; $mail->msgHTML(file_get_contents('news.php'), dirname(__FILE__)); $mail->send(); $mail->ClearAddresses(); $sqlEdicao_usuarios = " UPDATE newsletter SET enviado = 1 WHERE id = $id "; mysql_select_db($database_conexao, $conexao); $sqlEdicao_usuarios; $fim = mysql_query( $sqlEdicao_usuarios,$conexao ) or die ( "Erro alterando dados no Banco de Dados" ); }
Why the first while? I believe you can do everything from a single while...
– Rafael Withoeft
I get the Id and email in the first WHILE and try to send my newsletter by while($Row = $result->fetch_assoc())
– adventistapr
instead of doing the second while, you can put everything inside the first, it makes no sense to use 2 whiles
– RodrigoBorth
Add your code all to the first while, and modify your $id variables by $Row['id'], and $email by $Row['email']
– Rafael Withoeft