我正在尝试设置一个邮件脚本,它将首先从mysql中运行一个简单的选择,并在消息中使用这些数组变量。但是,所有变量都不会输出到消息体,只有一行变量。这是我的剧本:
$sql1 = "SELECT * FROM videos WHERE checked_out = '1'";
$result1 = $dbLink->query($sql1);
while($row1 = $result1->fetch_assoc()) {
$name = $row1['name'];
$tape_no = $row1['tape_no'];
$member_name = $row1['member_name'];
$date_out = date("F j, Y", strtotime($row1['date_out']));
}
//email function to administrator
$to = "nouser@mail.com";
$subject = "Daily Video Rental Summary";
$message = "$name $tape_no $date_out $member_name
======================================================================
PLEASE DO NOT REPLY TO THIS MESSAGE, AS THIS MAILBOX IS NOT MONITORED
======================================================================";
$from = "no_replies_please@mail.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
感谢任何有关此人分享的见解。 谢谢, --Matt
答案 0 :(得分:2)
请改为尝试:
$sql1 = "SELECT * FROM videos WHERE checked_out = '1'";
$result1 = $dbLink - > query($sql1);
while ($row1 = $result1 - > fetch_assoc()) {
$name = $row1['name'];
$tape_no = $row1['tape_no'];
$member_name = $row1['member_name'];
$date_out = date("F j, Y", strtotime($row1['date_out']));
//email function to administrator
$to = "nouser@mail.com";
$subject = "Daily Video Rental Summary";
$message = "$name $tape_no $date_out $member_name
======================================================================
PLEASE DO NOT REPLY TO THIS MESSAGE, AS THIS MAILBOX IS NOT MONITORED
======================================================================";
$from = "no_replies_please@mail.com";
$headers = "From:".$from;
mail($to, $subject, $message, $headers);
}
答案 1 :(得分:1)
那是因为你一直在覆盖变量,所以它只得到最后一个。您可能希望通过执行在while循环中构建$message
变量。这将发送1封电子邮件,包含所有内容
$sql1 = "SELECT * FROM videos WHERE checked_out = '1'";
$result1 = $dbLink->query($sql1);
while($row1 = $result1->fetch_assoc()) {
$name = $row1['name'];
$tape_no = $row1['tape_no'];
$member_name = $row1['member_name'];
$date_out = date("F j, Y", strtotime($row1['date_out']));
$message .= "$name $tape_no $date_out $member_name"
}
//email function to administrator
$to = "nouser@mail.com";
$subject = "Daily Video Rental Summary";
======================================================================
PLEASE DO NOT REPLY TO THIS MESSAGE, AS THIS MAILBOX IS NOT MONITORED
======================================================================";
$from = "no_replies_please@mail.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
答案 2 :(得分:0)
您在循环中覆盖变量,因此$name
等将包含循环后最后一行中找到的值。
您可以做的是在循环中构建您的消息:
$message = '';
while($row1 = $result1->fetch_assoc()) {
$message .= $row1[...]; // whatever you need
}
答案 3 :(得分:0)
那是因为你在循环中分配变量,但是在$ message变量 outside 循环中使用它们。所以你的$ message只包含最后一行/记录中的项目。尝试在while循环内的$ message变量中移动和追加值。
所以它可能是
while($row1 = $result1->fetch_assoc()) {
//assign vars here
$message .= "$name $tape_no $date_out $member_name\n";
}
$message = "$message
======================================================================
PLEASE DO NOT REPLY TO THIS MESSAGE, AS THIS MAILBOX IS NOT MONITORED
======================================================================";
//assign other variables
//mail()
希望有所帮助。
答案 4 :(得分:0)
试试这个:
$message = NULL;
$sql1 = "SELECT * FROM videos WHERE checked_out = '1'";
$result1 = $dbLink->query($sql1);
while($row1 = $result1->fetch_assoc()) {
$name = $row1['name'];
$tape_no = $row1['tape_no'];
$member_name = $row1['member_name'];
$date_out = date("F j, Y", strtotime($row1['date_out']));
$message .= "$name $tape_no $date_out $member_name" }
并删除循环下的其他$ message变量。请注意。 $消息中的=之前。这告诉php不断添加$ message