How to take data from a Select Mysqli and send newsletter

Asked

Viewed 2,601 times

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...

  • I get the Id and email in the first WHILE and try to send my newsletter by while($Row = $result->fetch_assoc())

  • instead of doing the second while, you can put everything inside the first, it makes no sense to use 2 whiles

  • Add your code all to the first while, and modify your $id variables by $Row['id'], and $email by $Row['email']

1 answer

4


Test as follows:

I put the second while code in the first so eliminating it (because there was no need for it) and simplifying its code as well.

What happened before: the first while only kept an id and email in the variable $id, $email, which probably caused the problems (This when returning some result...).

$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()) {
    $mail->setFrom('[email protected]', 'Newsletter');
    $mail->addAddress($row['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 = $row['id']";// ou jogue em uma variável e substitua por ela.... ($id = $row['id'])
    mysql_select_db($database_conexao, $conexao);
    $sqlEdicao_usuarios;
    $fim = mysql_query( $sqlEdicao_usuarios,$conexao ) or die ( "Erro alterando dados no Banco de Dados" ); 
}
  • Thank you @Rafael Withoeft, was of great help.

  • @Sergio Pronto... I think this is good, what do you think? adventistapr.. for nothing.

  • Hi @Rafael Withoeft, how can I get mysql_num_rows?

  • $Count = mysqli_num_rows($result); or: $result->num_rows; If you need documentation: http://php.net/manual/en/mysqli-result.num-rows.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.