[英]Query MySQL for an email address and send an email to that address
I am trying to retrieve an email address from a table where the username is a known value. 我试图从用户名是已知值的表中检索电子邮件地址。 That value will be passed from the previous page but I have made it a constant in this example.
该值将从上一页传递,但我在此示例中将其设为常量。
I don't know how to give the result a variable name and call that variable later for the "To" address. 我不知道如何给结果一个变量名,并稍后为“To”地址调用该变量。
Any help is appreciated. 任何帮助表示赞赏。
<?php
$servername = "xxxx";
$username = "xxxx";
$password = "xxxx";
$dbname = "xxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$SLT_YeerLead = "B.Baggins";
$sql = "SELECT YeerLeadMail FROM t_staffyeerlead WHERE YeerLead='$SLT_YeerLead'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "" . $row["YeerLeadMail"]. "";
}
} else {
echo "0 results";
}
$conn->close();
echo "<br><br>";
$to = "??????????????";
$subject = "Subject";
$message = "blah blah blah<br><br>";
$header = "From: administrator@mail.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true )
{
echo "An E-Mail was sent to the Year Lead <b>$SLT_YeerLead</b><br><br>";
}
else
{
echo "An E-Mail could not be sent to the Year Lead <b>$SLT_YeerLead</b><br><br>";
echo "<br><br><br><br><br><br>";
echo "<b>Please contact ICT Support</b><br><br>";
echo "Call XXXX<br><br>";
echo "E-Mail: help@mail.com<br><br>";
}
mysql_close($con); ?>
Thanks 谢谢
Declare the $email inside the while loop as you only have a one row result and assign it to the $to or declare and assign it for cleaner code. 在while循环中声明$ email,因为您只有一行结果并将其分配给$ to或声明并将其分配给更干净的代码。
if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "" . $row["YeerLeadMail"]. ""; $email = $row["YeerLeadMail"]; }
$to = $email;
or 要么
if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "" . $row["YeerLeadMail"]. ""; $to = $row["YeerLeadMail"]; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.