我将生日祝福电子邮件发送给生日在当前日期以及接下来的07天内的所有用户。
但是,电子邮件正文没有找到发送该电子邮件的用户的第一个名字。
电子邮件正文说:你好
相反应该说:Hello {first name}
代码是:
<?php
$query_birth = mysql_query("select email_address from table_name where DATE_ADD(birth_date, INTERVAL YEAR( CURDATE( ) ) - YEAR(birth_date) + IF( DAYOFYEAR( CURDATE( ) ) > DAYOFYEAR(birth_date) , 1, 0 ) YEAR ) BETWEEN CURDATE( ) AND DATE_ADD( CURDATE( ) , INTERVAL 7 DAY )");
$count_birth = mysql_num_rows($query_birth);
if(!empty($count_birth)) {
while($row_birth = mysql_fetch_array($query_birth)) {
$headers = 'From: admin@example.com' . "\r\n";
$message = 'Hello '.$row_birth['user_first_name'].'';
mail($row_birth['email_address'], 'Happy Birthday', $message, $headers);
}
}
?>
不确定我做了什么错误?
答案 0 :(得分:1)
您只是选择用户电子邮件,而不是用户名。
更改
$query_birth = mysql_query("select email_address from table_name where DATE_ADD(birth_date, INTERVAL YEAR( CURDATE( ) ) - YEAR(birth_date) + IF( DAYOFYEAR( CURDATE( ) ) > DAYOFYEAR(birth_date) , 1, 0 ) YEAR ) BETWEEN CURDATE( ) AND DATE_ADD( CURDATE( ) , INTERVAL 7 DAY )");
到
$query_birth = mysql_query("select email_address, user_first_name from table_name where DATE_ADD(birth_date, INTERVAL YEAR( CURDATE( ) ) - YEAR(birth_date) + IF( DAYOFYEAR( CURDATE( ) ) > DAYOFYEAR(birth_date) , 1, 0 ) YEAR ) BETWEEN CURDATE( ) AND DATE_ADD( CURDATE( ) , INTERVAL 7 DAY )");
答案 1 :(得分:-1)
您应该选择all以使其更容易,因此您不会遇到相同的错误
$query_birth = mysql_query("SELECT * FROM table_name WHERE DATE_ADD(birth_date, INTERVAL YEAR( CURDATE( ) ) - YEAR(birth_date) + IF( DAYOFYEAR( CURDATE( ) ) > DAYOFYEAR(birth_date) , 1, 0 ) YEAR ) BETWEEN CURDATE( ) AND DATE_ADD( CURDATE( ) , INTERVAL 7 DAY )");