向不同用户发送邮件,其中电子邮件地址来自 Mysql

我想从具有条件的表中获取所有行,然后向指定的用户发送电子邮件。表中有 2 个用户。如果我将 sql 语句放在 sql 语句中,则它不起作用,只有第一个用户才能收到邮件和邮件。我确信这很容易,但我是 php 和 mysql 初学者,所以请帮助我。


表中有 2 个用户。


<?php 

require'../includes/database.php'; 


    $sql = "SELECT * FROM users";  

$result = $conn->query($sql);  


if ($result->num_rows > 0) {  


    // output data of each row  

    while($row = $result->fetch_assoc()) {  

        $fname = $row["fname"];  

        $nname = $row["lname"];  

        $mailadresse = $row["email"];  

        $user2 = $fname." ".$nname;  

        echo $user2;  


        $sql = "SELECT * FROM iks_open, users WHERE Datum <= '$neudate' AND  pverantwortung = '$user2'";  

        $result = $conn->query($sql);  


        if ($result->num_rows > 0) {  

            $mailhost       = "$server";  //SMTP Host name  

            $mailsmtpauth   = true;  

            $mailusername   = "$user"; // SMTP Login  

            $mailpassword   = "$pass"; // SMTP Password  



            require_once('../includes/Mailer/class.phpmailer.php');  

            require_once('../includes/Mailer/class.smtp.php');  



            $mail = new PHPMailer();   


            $mail->IsSMTP();

            $mail->Host       = $mailhost;  

            $mail->Port       = $port;  

            $mail->SMTPDebug  = 1; // Kann man zu debug Zwecken aktivieren  

            $mail->SMTPAuth   = true;  

            $mail->Username   = $mailusername;  

            $mail->Password   = $mailpassword;  

            $mail->SMTPSecure = $sec;  



            $frommail = "$email";  

            $mail->SetFrom($frommail, 'IKS-System');  


            $address = "$mailadresse";  

            $mail->AddAddress($address);  


            $mail->Subject = "Offene IKS-Aufgaben";  

            $mail->Body = "Sie haben offene Aufgaben. Bitte einloggen und erledigen";  


            if(!$mail->Send()) {  


            $lcontact = 1;  

            } else {  

            $lcontact = 2;  


            }  


            }else {  

                echo "Alles gut";  

            }  


    }  

}  



?>  



qq_笑_17
浏览 194回答 2
2回答

呼唤远方

您正在while 语句中覆盖$result变量。小心你的变量,你不会有那样的麻烦。

茅侃侃

我认为,在 while 循环内发生的数据库获取正在覆盖 $result 对象。尝试这个<?php&nbsp;require'../includes/database.php';&nbsp;$sql = "SELECT * FROM users";&nbsp;&nbsp;$result = $conn->query($sql);&nbsp;&nbsp;if ($result->num_rows > 0) {&nbsp;&nbsp;// output data of each row&nbsp;&nbsp;while($row = $result->fetch_assoc()) {&nbsp;&nbsp;&nbsp; &nbsp; $fname = $row["fname"];&nbsp;&nbsp;&nbsp; &nbsp; $nname = $row["lname"];&nbsp;&nbsp;&nbsp; &nbsp; $mailadresse = $row["email"];&nbsp;&nbsp;&nbsp; &nbsp; $user2 = $fname." ".$nname;&nbsp;&nbsp;&nbsp; &nbsp; echo $user2;&nbsp;&nbsp;&nbsp; &nbsp; $query = "SELECT * FROM iks_open, users WHERE Datum <= '$neudate' AND&nbsp; pverantwortung = '$user2'";&nbsp;&nbsp;&nbsp; &nbsp; $details = $conn->query($sql);&nbsp;&nbsp;&nbsp; &nbsp; if ($details->num_rows > 0) {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $mailhost&nbsp; &nbsp; &nbsp; &nbsp;= "$server";&nbsp; //SMTP Host name&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $mailsmtpauth&nbsp; &nbsp;= true;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $mailusername&nbsp; &nbsp;= "$user"; // SMTP Login&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $mailpassword&nbsp; &nbsp;= "$pass"; // SMTP Password&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; require_once('../includes/Mailer/class.phpmailer.php');&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; require_once('../includes/Mailer/class.smtp.php');&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $mail = new PHPMailer();&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $mail->IsSMTP();&nbsp; &nbsp; &nbsp; &nbsp; $mail->Host&nbsp; &nbsp; &nbsp; &nbsp;= $mailhost;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $mail->Port&nbsp; &nbsp; &nbsp; &nbsp;= $port;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $mail->SMTPDebug&nbsp; = 1; // Kann man zu debug Zwecken aktivieren&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $mail->SMTPAuth&nbsp; &nbsp;= true;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $mail->Username&nbsp; &nbsp;= $mailusername;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $mail->Password&nbsp; &nbsp;= $mailpassword;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $mail->SMTPSecure = $sec;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $frommail = "$email";&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $mail->SetFrom($frommail, 'IKS-System');&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $address = "$mailadresse";&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $mail->AddAddress($address);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $mail->Subject = "Offene IKS-Aufgaben";&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $mail->Body = "Sie haben offene Aufgaben. Bitte einloggen und erledigen";&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if(!$mail->Send()) {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $lcontact = 1;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $lcontact = 2;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }else {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "Alles gut";&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;}&nbsp;&nbsp;}?>
打开App,查看更多内容
随时随地看视频慕课网APP