MYSQL在循环内调用查询会停止循环

时间:2015-05-05 20:14:02

标签: php mysql

好的我想要的是通过数据库并发送电子邮件到每个电子邮件,其数据库存储时间和日期低于当前日期,其发送值为0.什么工作是它通过电子邮件循环并发送然而,我想要做的是将数据库中的发送值从0设置为1,这样如果我将来运行该程序,它就不会再将电子邮件发送给我已发送电子邮件的人。我尝试在我的while循环中将发送值更新为1但是当我执行我的文件时它只在一封电子邮件后停止,因此它不会循环通过数据库

<?php
include_once("db.php");
$date = new DateTime();
//echo $date->format('Y-m-d H:i:s')

$query = "SELECT timedate, email, sent, msgid  FROM mailer";
$result = mysql_query($query);
echo "<table>";
while($row = mysql_fetch_array($result)){
       $tot = $row['timedate'];
       $ema = $row['email'];
       $sendflag = $row['sent'];
       $mess = $row['msgid'];

    if(strtotime($tot) > time()) {
    //echo "<tr><td>" .$row['timedate']."</td><td>";
    echo"database dates higher then now dates" . "<br>";
    echo "<tr><td>" .$row['email']."</td><td>" ."<br>";
   echo "<tr><td>" .$row['timedate']."</td><td>" ."<br>";
      echo "<tr><td>" .$row['sent']."</td><td>" ."<br>";
}
else {
    echo"database dates lower then now dates" . "<br>";
    echo "<tr><td>" .$row['email']."</td><td>". "<br>";
       echo "<tr><td>" .$row['timedate']."</td><td>" ."<br>";
          echo "<tr><td>" .$row['sent']."</td><td>" ."<br>";
$subject = "This is subject";
   $message = "This is simple text message.";
   $header = "From:abc@somedomain.com \r\n";
   $to = $row['email'];
   $retval = mail ($to,$subject,$message,$header);
   if( $retval == true )  
   {
      echo "Message sent successfully..." ."<br>";
   }
   else
   {
      echo "Message could not be sent..." ."<br>";
   }

    //$sql = "UPDATE mailer SET sent = 1 WHERE msgid = $mess";
    $query = "UPDATE mailer SET sent = 1 WHERE msgid = $mess";
    $result = mysql_query($query);





}








}
mysql_close();
?>

1 个答案:

答案 0 :(得分:0)

例如,将$ result的结果重命名为$ result2。您正在覆盖正在循环的变量。