【问题标题】:Iteratre through rows, update and send email遍历行、更新和发送电子邮件
【发布时间】:2013-11-13 01:46:59
【问题描述】:

我在遍历行、更新字段、然后根据日期发送电子邮件时遇到问题。我也可以让第一行发送电子邮件,但我不知道为什么我不能让其他行发送电子邮件。我可以显示返回了三行但我无法发送三封电子邮件。代码如下,提前感谢您的帮助。

<?php




    for($numOfRows = 1; $numOfRows > 0;){       

    $sql = "SELECT * FROM service WHERE boosterDate BETWEEN CURDATE() AND CURDATE() + INTERVAL 30 DAY";

if(!$result = $con->query($sql)){
    die('There was an error running the query [' . $con->error . ']');  
    }

    $numOfRows = $result->num_rows;
    //%result->store_result();
    $numOfRows = var_dump($numOfRows);
    //echo $numOfRows;

//if($numOfRows > 0){

    //var_dump($numOfRows);

while($row = $result->fetch_array()){
        //Get all the rows and store them in an array   
    $firstQueryRows[] = $row;

}

foreach($firstQueryRows as $row){

    //do a new query with $row
    $serviceID = $row['serviceID'];
    $patientID = $row['patientID'];
    $serviceName = $row['serviceName'];
    $price = $row['price'];
    $quantity = $row['quantity'];
    $boosterDate = $row['boosterDate'];
    $totalPrice = $row['totalPrice'];
    $rabiesTagNumber = $row['rabiesTagNumber'];
    $microChipNumber = $row['microChipNumber'];
    $rabiesDose = $row['rabiesDose'];
    $rabiesVacType = $row['rabiesVacType'];

    $rabiesVacExpDate = $row['rabiesVacExpDate'];
    $rabiesSerialNumber = $row['rabiesSerialNumber'];
    $rabiesManufacturer = $row['rabiesManufacturer'];
    $rabiesVacDate = $row['rabiesVacDate'];
    $rabiesVacDueBy = $row['rabiesVacDueBy'];
    $licenseNumber = $row['licenseNumber'];
    $isThisARabiesCert = $row['isThisARabiesCert'];
    $vacDate = $row['vacDate'];
    $email30 = $row['email30'];
    $email60 = $row['email60'];
    $email90 = $row['email90'];     
    //echo $serviceID . "  ";
    $sql = "UPDATE service SET email30 = 30 WHERE serviceID = $serviceID";

    if(!$result = $con->query($sql)){
    die('There was an error running the query [' . $con->error . ']');  
    }

    }//End Foreach



    //echo $patientID;


    $sql = "SELECT * FROM service WHERE email30 = 30";
    if(!$result = $con->query($sql)){
    die('There was an error running the query [' . $con->error . ']');  
    }

    while($row = $result->fetch_array()){
        //Get all the rows and store them in an array   
    $firstQueryRows[] = $row;

    $patientID = $row['patientID'];
    //echo $patientID;

}

    foreach($firstQueryRows as $row){



        $sql = "SELECT * FROM patientInformation WHERE patientID = $patientID";
    if(!$result = $con->query($sql)){
    die('There was an error running the query [' . $con->error . ']');  
    }       

    }

    while($row = $result->fetch_array()){
        $firstQueryRows[] = $row;
        $clientID = $row['clientID'];
        $maleFemale = $row['maleFemale'];
        $species = $row['species'];
        $ageYears = $row['ageYears'];
        $ageMonths = $row['ageMonths'];
        $weight = $row['weight'];
        $dogBreed = $row['dogBreed'];
        $name = $row['name'];
        $catBreed = $row['catBreed'];
        $colors = $row['colors'];
        $illnessLast30Days = $row['illnessLast30Days'];
        $explainIllness = $row['explainIllness'];


        //echo $clientID;   

    }

    foreach($firstQueryRows as $row){

        $sql = "SELECT * FROM clientInformation WHERE clientID = $clientID";
    if(!$result = $con->query($sql)){
    die('There was an error running the query [' . $con->error . ']');  
    }

    }

    while($row = $result->fetch_array()){

        $firstName = $row['firstName'];
        $lastName = $row['lastName'];
        $address = $row['address'];
        $city = $row['city'];
        $zip = $row['zip'];
        $phone = $row['phone'];
        $cell = $row['cell'];
        $state = $row['state'];
        $email = $row['email'];




    }

    $to = $email;
    $subject = "Just a friendly reminder from the Shot Spot";


    $message = "Greetings " . $firstName . " " .    $lastName . "\r\n" . "\r\n" . "\r\n"  .
        "Thank you for choosing the Shot Spot for all of your animals healthcare needs.  This is a friendly reminder that  
                            " . $name . " is ready for their booster on " . $boosterDate . "\r\n" . "\r\n" . "\r\n" . 
                            "Thank you for choosing the Shot Spot";


    $headers = 'From: info@txshotspot.com';

    mail($to, $subject, $message, $headers);



}//end for loop


?>

【问题讨论】:

    标签: php mysql email loops for-loop


    【解决方案1】:

    你没有遍历你的 for 循环,你的代码有点混乱:p。 你想发送三封电子邮件试试这个:

    for($numOfRows = 0; $numOfRows <= 3; $numOfRows++)
    

    【讨论】:

    • 感谢您的反馈。我想发送与找到的行一样多的电子邮件。此查询中恰好有三个,因此按照您设置的方式设置 for 循环将无法正常工作。另外,我知道它的编码不是最好的。对 php 编码仍然很陌生。您能否就我正在寻找的最佳方法提供一些建议。我有三张表,我将 id 与所有三张表匹配,同时还要检查今天 30 天内的日期。那时我需要向与搜索条件匹配的任何行发送一封电子邮件。我可以发送一个,但到此为止。
    • 是的,我可以给你一些建议:
    • 是的,我可以给你一些建议。你能把三张桌子的结构放在一边让我看看你在说什么吗?而且由于您不知道要发送多少封电子邮件,因此在这种情况下,While 循环将完美运行。我还注意到:您应该使用准备好的语句或在每个语句上更改您的代码如下。这是一个示例:旧代码: $sql = "SELECT * FROM clientInformation WHERE clientID = $clientID";新代码:$sql = 'SELECT * FROM clientInformation WHERE clientID = '.$clientID;祝你好运,让我知道这是否有效。
    猜你喜欢
    • 1970-01-01
    • 2023-01-15
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多