while循环在第一次迭代后停止

时间:2015-07-29 10:35:42

标签: php mysqli while-loop warnings

我正在运行一个脚本,如果当前日期与数据库中的日期匹配,则构建并向客户发送电子邮件。

我的问题是while循环在第一次迭代后停止并输出以下警告:

  

警告:mysqli_stmt :: fetch()[mysqli-stmt.fetch]:无法获取   第17行的文件路径中的mysqli_stmt

第一次迭代完美无缺,并正确发送电子邮件。我将整个循环粘贴在下面,因为我不确定错误发生在哪里..

 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();

1 个答案:

答案 0 :(得分:0)

找到解决方案,代码中包含的电子邮件构建器文件包含另一个 $ stmt 变量,其变量名称与while循环基于相同,强制它崩溃...