每当我调用php脚本时,是否可以从mysql数据库中选择一个新行?例如,每次调用以下php脚本时,我都需要从mysql数据库中获取新的电子邮件地址。我知道mysqls'ORDER BY RAND ()
函数;在这里,我需要根据他们的输入日期(即ORDER BY created_on DESC
)获取每一行。
// Add recepients from a datasoutce iteratively
$emails = mysql_query('SELECT email from recepients ORDER BY created_on DESC LIMIT 1') or die;
while($row = mysql_fetch_assoc($emails))
{
message->addTo($row['email']);
}
编辑:我将使用cron job(Windows中的预定任务)来运行我的php脚本,我需要一个解决方案来逐个选择行(不是随机),因为我的php被调用。
答案 0 :(得分:1)
如果允许,请将列添加到表:
alter table recepients add column uts_sent BIGINT NOT NULL;
发送电子邮件后更新表格:
$limit= 1; //fetch $limit rows per page
$now = mktime(); //current unix time stamp
// Add recepients from a datasource iteratively
$emails = mysql_query("SELECT email from recepients WHERE uts_sent<$now ORDER BY created_on DESC LIMIT $limit") or die;
while($row = mysql_fetch_assoc($emails))
{
$message->addTo($row['email']);
$message->send();
$email_escaped = mysql_real_escape_string($row['email']);
mysql_query("update recepients set uts_sent = $now where email ='$email_escaped'") or die;
}