我首先在检查sql查询后尝试自动化电子邮件警报。
由于我对php不太熟悉,因此我找到了一个代码,但第29行会返回错误。
while($row = mysqli_fetch_array($result))
{
$message = {$row['User Name'] . " " . $row['Email']. " " . $row['Course']. " " . $row['Attempt']. " " . $row['Grade']. " " . $row['Finish time']\n};
}
请指教。您可以在下面找到所有代码。 谢谢。
<?php
$con=mysqli_connect("localhost","user","pass","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT
us.username AS 'User Name',
us.email AS 'Email',
qu.name AS 'Course',
quat.attempt AS 'Attempt',
quat.sumgrades AS 'Grade',
FROM_UNIXTIME(quat.timefinish,'%M %D %Y %h:%i:%s') AS 'Finish time'
FROM mdl_quiz_attempts AS quat
JOIN mdl_user AS us ON quat.userid=us.id
JOIN mdl_quiz AS qu ON quat.quiz=qu.id
WHERE (quat.attempt = 3 OR quat.attempt = 6) AND quat.sumgrades < 80 AND quat.timefinish > (unix_timestamp(now())-300)
ORDER BY us.username,qu.name,quat.attempt");
if($result->num_rows>=1) {
$email='dc@antaea.com';
$subject = "E-School failed attempts";
$message='One or more users failed quiz three or more times:\n\n';
while($row = mysqli_fetch_array($result))
{
$message = {$row['User Name'] . " " . $row['Email']. " " . $row['Course']. " " . $row['Attempt']. " " . $row['Grade']. " " . $row['Finish time']\n};
}
if(mail($email, $subject, $message)) {
//mail successfully sent
} else {
//mail unsuccessful
}
}
mysqli_close($con);
?>
答案 0 :(得分:3)
尝试(最后用\ n作为字符串):
while($row = mysqli_fetch_array($result)){
$message = {$row['User Name'] . " " . $row['Email']. " " . $row['Course']. " " . $row['Attempt']. " " . $row['Grade']. " " . $row['Finish time'] . "\n"};
}
转义字符不能与单引号一起使用,您必须加倍单引号,如“\ n”