我想从一个条件表中获取所有行,然后将电子邮件发送给分配的用户。表格中有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";
}
}
}
?>
答案 0 :(得分:1)
您正在while语句中覆盖 $ result 变量。小心变量,这样就不会有麻烦了。
答案 1 :(得分:1)
我认为,发生在while循环内的数据库获取正在覆盖$ result对象。
尝试一下
<?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;
$query = "SELECT * FROM iks_open, users WHERE Datum <= '$neudate' AND pverantwortung = '$user2'";
$details = $conn->query($sql);
if ($details->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";
}
}
}
?>