2015-07-29 3 views
1

Я запускаю скрипт, который строит и отправляет письма клиентам, если текущая дата соответствует дате из базы данных.Пока цикл останавливается после первой итерации

Моя проблема заключается в том, что цикл в то время как останавливается после первой итерации и выводит следующее предупреждение:

Предупреждения: mysqli_stmt :: выборки() [MySQLi-stmt.fetch]: не удался выборка mysqli_stmt в FilePath по линии 17

Первая итерация работает безотказно и отправляет сообщение электронной почты правильно. Я вставив весь в то время как цикл ниже как я не уверен, где происходит ошибка ..

date_default_timezone_set('Europe/Stockholm'); 
$today = date('Y-m-d'); 
$sent = '0'; 

    // fech all info where firstdate is today 
if ($stmt = $mysqli->prepare("SELECT ex_id,ex_type,ex_comp,ex_firstname,ex_lastname,ex_email,ex_template,ex_attachments FROM execute WHERE ex_firstDate = ? AND ex_sent = ?")) { 
    $stmt->bind_param('si',$today,$sent); 
    if ($stmt->execute()) { 
     $stmt->store_result(); 
     $stmt->bind_result($first_id,$first_type,$first_comp,$first_firstname,$first_lastname,$first_email,$first_template,$first_attachments); 

      // loop through 
     while ($row = $stmt->fetch()) { 
      if (empty($first_id) || $first_id == 0) { 
       $current = date('Y-m-d H:i:s')." - N/A - NO RECIPIENT FIRST DATE :\r\n"; 
       fwrite($handle, $current); 
       continue; 
      } 

      if ($first_type == 1) { 
       $stmt_b = $mysqli->prepare("SELECT unsub_email FROM unsubscribe WHERE unsub_program = 1"); 
       $stmt_b->execute(); 
       $stmt_b->store_result(); 
       $stmt_b->bind_result($unsub_email); 

       while ($row_unsub = $stmt_b->fetch()) { 
        if ($first_email == $unsub_email) { 
         $current = date('Y-m-d H:i:s')." - Opt-out - PROGRAM #".$first_id." :\r\n"; 
         fwrite($handle, $current); 
         continue 2; 
        } 
       } 
      } elseif ($first_type == 2) { 
       $stmt_b = $mysqli->prepare("SELECT unsub_email FROM unsubscribe WHERE unsub_cat = 1"); 
       $stmt_b->execute(); 
       $stmt_b->store_result(); 
       $stmt_b->bind_result($unsub_email); 

       while ($row_unsub = $stmt_b->fetch()) { 
        if ($first_email == $unsub_email) { 
         $current = date('Y-m-d H:i:s')." - Opt-out - CATEGORY #".$first_id." :\r\n"; 
         fwrite($handle, $current); 
         continue 2; 
        } 
       } 
      } 

      $current = date('Y-m-d H:i:s')." - Success - GET RECIPIENT FIRST #".$first_id." :\r\n"; 
      fwrite($handle, $current); 

      $id = $first_template; 
      $preview = false; 

      $builder_type = $first_type; 

      if (empty($first_comp)) { 
       $builder_comp = ''; 
      } else { 
       $builder_comp = $first_comp; 
      } 

      if (empty($first_firstname)) { 
       $builder_firstname = ''; 
      } else { 
       $builder_firstname = $first_firstname; 
      } 

      if (empty($first_lastname)) { 
       $builder_lastname = ''; 
      } else { 
       $builder_lastname = $first_lastname; 
      } 

      if (empty($first_email)) { 
       $builder_email = ''; 
      } else { 
       $builder_email = $first_email; 
      } 

        //Create a new PHPMailer instance 
      require_once(__DIR__.'/../email/email_builder.php'); 

      $mail = new PHPMailer; 

      $mail->CharSet = 'UTF-8'; 
      $mail->Encoding = '8bit'; 

      // To load the Swedish version 
      $mail->setLanguage('se', __DIR__.'../email/PHPMailer-master/language/'); 

      //Tell PHPMailer to use SMTP 
      $mail->isSMTP(); 

      //Enable SMTP debugging 
      // 0 = off (for production use) 
      // 1 = client messages 
      // 2 = client and server messages 
      $mail->SMTPDebug = 0; 

      //Ask for HTML-friendly debug output 
      $mail->Debugoutput = 'html'; 

      //Set the hostname of the mail server 
      $mail->Host = 'HOST'; 

      //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission 
      $mail->Port = 587; 

      //Set the encryption system to use - ssl (deprecated) or tls 
      $mail->SMTPSecure = 'tls'; 

      //Whether to use SMTP authentication 
      $mail->SMTPAuth = true; 

      //Username to use for SMTP authentication - use full email address for gmail 
      $mail->Username = "USERNAME"; 

      //Password to use for SMTP authentication 
      $mail->Password = "PASSWORD"; 

      //Set who the message is to be sent from 
      $mail->setFrom($db_fromEmail, $db_fromName); 

      //Set an alternative reply-to address 
      $mail->addReplyTo($db_respondEmail, $db_respondName); 

      //Set the subject line 
      $mail->Subject = $db_subject; 

      //Read an HTML message body from an external file, convert referenced images to embedded, 
      //convert HTML into a basic plain-text alternative body 
      $mail->msgHTML($html); 

      // create alt plain text body 
      $plain_text = Html2Text\Html2Text::convert($db_content.$plain_text_footer); 

      $mail->AltBody = $plain_text; 

      $mail->addAddress($first_email); 

      if (!empty($first_attachments) && $first_attachments != 0) { 
       $array_attach = explode(',', $first_attachments); 
       $array_attach = array_map('trim', $array_attach); 

       foreach ($array_attach as $key) { 
        if ($stmt_a = $mysqli->prepare("SELECT attach_name,attach_path,attach_type FROM attachments WHERE attach_id = ?")) { 
         $stmt_a->bind_param('i',$key); 
         if ($stmt_a->execute()) { 
          $current = date('Y-m-d H:i:s')." - Success - ADD ATTACHMENT FIRST #".$first_id."\r\n"; 
          fwrite($handle, $current); 

          $stmt_a->store_result(); 
          $stmt_a->bind_result($attach_name,$attach_path,$attach_type); 
          $stmt_a->fetch(); 

          $full_path = __DIR__.'/../'.$attach_path; 
          $full_name = $attach_name.$attach_type; 

          $mail->addAttachment($full_path,$full_name); 
         } else { 
          $error = $stmt_a->error; 
          $current = date('Y-m-d H:i:s')." - Error - ADD ATTACHMENT FIRST #".$first_id." : ".$error."\r\n"; 
          fwrite($handle, $current); 
         } 
        } else { 
         $error = $stmt_a->error; 
         $current = date('Y-m-d H:i:s')." - Error - ADD ATTACHMENT FIRST #".$first_id." : ".$error."\r\n"; 
         fwrite($handle, $current); 
        } 
       } 
      } 

      if($mail->send()) { 
       $current = date('Y-m-d H:i:s')." - Success - SENT RECIPIENT FIRST ".$first_id."\r\n"; 
       fwrite($handle, $current); 

       $sent_db = '1'; 

       if ($stmt_s = $mysqli->prepare("UPDATE execute SET ex_sent = ? WHERE ex_id = ?")) { 
        $stmt_s->bind_param('ii',$sent_db,$first_id); 
        if ($stmt_s->execute()) { 
         $current = date('Y-m-d H:i:s')." - Success - SENT UPDATE DB FIRST #".$first_id."\r\n"; 
         fwrite($handle, $current); 
        } else { 
         $error = $stmt_s->error; 
         $current = date('Y-m-d H:i:s')." - Error - SENT UPDATE DB FIRST #".$first_id." : ".$error."\r\n"; 
         fwrite($handle, $current); 
        } 
       } else { 
        $error = $stmt_s->error; 
        $current = date('Y-m-d H:i:s')." - Error - SENT UPDATE DB FIRST #".$first_id." : ".$error."\r\n"; 
        fwrite($handle, $current); 
       } 
      } else { 
       $error = $mail->ErrorInfo; 
       $current = date('Y-m-d H:i:s')." - Error - SENT RECIPIENT FIRST #".$first_id." : ".$error."\r\n"; 
       fwrite($handle, $current); 
      } 

      $mail->clearAddresses(); 
      $mail->clearAttachments(); 
     } 
    } else { 
     $error = $stmt->error; 
     $current = date('Y-m-d H:i:s')." - Error - GET TEMPLATE FIRST : ".$error."\r\n"; 
     fwrite($handle, $current); 
    } 
} else { 
    $error = $stmt->error; 
    $current = date('Y-m-d H:i:s')." - N/A - GET TEMPLATE FIRST : ".$error."\r\n"; 
    fwrite($handle, $current); 
} 

$stmt->close(); 
+0

Привет, я подозреваю, что вы где-то убиваете набор данных. просто используйте mysqli_num_rows, чтобы убедиться, что у вас более 1 строки – volkinc

ответ

0

Найдено решение, файл электронной почты строитель включен в код, содержащийся еще $ STMT переменную с тем же имя переменной, когда были созданы петли while, заставляя ее сбой ...

Смежные вопросы