我一直在到处搜索stackoverflow,但就是找不到解决我问题的方法。我正试图建立一个php脚本(用于cron作业),在我的数据库中检测当前日期是否已满足并向该用户发送电子邮件。不管什么原因,我都不能让它运行。也许你们当场就看到了?
表项如下:
date =要检查的日期Email =提醒要发送到的邮箱地址
下面是php脚本的代码:<?php
header("Content-type: text/plain");
// OPEN DATA BASE
define("HOSTNAME");
define("USERNAME");
define("PASSWORD");
define("DATABASE");
mysql_connect(hostname, username, password) or die("Connection to database failed!");
mysql_select_db(database);
taskdeadline();
// MAIN FUNCTION
function taskdeadline() {
$result = mysql_query("SELECT * FROM members WHERE date = NOW()");
while ($row = mysql_fetch_array($result)){
$task = row['task'];
$mail_to = $row['email'];
if (!empty($mail_to)){
sendEmail($mail_to);
echo "E-Mail send to $mail_to. 'n";
}
}
}
// SEND EMAIL FUNCTION
function sendEmail($mail_to, $task) {
$from = "my@emailaddress.com";
$message = "Hello! You are the user with the email address $email and the task $task. Your deadline has been reached.";
$headers = 'From: '.$from."'r'n" .
'Reply-To:'.$_POST['email']."'r'n" .
"Content-Type: text/html; charset=iso-8859-1'n".
'X-Mailer: PHP/' . phpversion();
mail($mail_to, $message, $headers);
}
?>
$task = row['task'];
必须$task = $row['task'];
日期的数据类型是什么?如果你想计算它,那么它必须是一个时间戳或datetime或日期字段。
如果要匹配日期类型字段,则使用CURDATE()而不是NOW()。我也会检查
WHERE date >= CURDATE()
这样你就可以捕捉到任何已经通过的