简体   繁体   English

如何在PHP中的其他while循环中运行while循环?

[英]How can I run while loop inside other while loop in PHP?

I'm trying to make PHP code which will automatically send email every night to all customers which have email send activated in MySQL. 我正在尝试制作PHP代码,该代码将每晚自动向所有在MySQL中激活了电子邮件发送功能的客户发送电子邮件。 Email will contain PDF file which will have delivery data from MySQL. 电子邮件将包含PDF文件,该文件将包含来自MySQL的传递数据。

I have sorted out how to send mail every night and how to create PDF from MySQL data. 我已经整理出了每天晚上如何发送邮件以及如何从MySQL数据创建PDF。 Problem is when I run script it will only send email to first customer which have email send activated (sendEmail = 1). 问题是当我运行脚本时,它将仅向已激活电子邮件发送功能的第一位客户发送电子邮件(sendEmail = 1)。 So it seems that PHP is running first while loop only once. 因此,似乎PHP首先在while循环中运行一次。 There is second while loop when data is collected from MySQL to PDF file. 当数据从MySQL收集到PDF文件时,存在第二个while循环。 But it seems that PHP will also stop first loop after second loop has been ran. 但似乎PHP也会在运行第二个循环后停止第一个循环。

Here is while loops. 这是while循环。 I have took away all PDF file creator and email code: 我已经带走了所有PDF文件创建者和电子邮件代码:

// Check if Email is activated
try {
  $resultsCustomerEmail = $db->query("
            SELECT id, name, email
            FROM customer
            WHERE sendEmail = 1
            ");
} catch (Exception $e) {
  echo "Data could not be retrieved from the database - customer table";
  exit;
}

while ($tempDBCustomerEmail = $resultsCustomerEmail->fetch(PDO::FETCH_ASSOC)) {

  $startdate = date('Y-m-d');
  $enddate = date('Y-m-d');

  try {
    $results = $db->query("
              SELECT delivery.id, date_format(delivery.timestamp,'%d.%m.%Y %H:%i:%s') as timestamp
              FROM delivery
              WHERE delivery.customer = " . $tempDBCustomerEmail['id'] . " AND
                date(delivery.timestamp) BETWEEN '" . $startdate . "' AND '" . $enddate . "'
              ORDER BY delivery.id ASC
              ");
  } catch (Exception $e) {
    echo "Data could not be retrieved from the database";
    exit;
  }

  // Data
  while ($tempDB = $results->fetch(PDO::FETCH_ASSOC)) {
    $timestamp = $tempDB['timestamp'];
    $deliveryId = $tempDB['id'];

  }
}

?>

Does anyone have any suggestions what I should change so that first while loop is ran as many times as first MySQL query is true. 没有人有任何建议我应该改变什么,以便第一个while循环运行的次数与第一个MySQL查询为真的次数相同。 Thanks! 谢谢!

One loop 一圈

Well the good news is that you don't need two loops here 好消息是您这里不需要两个循环

SELECT customer.id, customer.name, customer.email,
  delivery.id, date_format(delivery.timestamp,'%d.%m.%Y %H:%i:%s') as timestamp
          FROM delivery INNER JOIN customer ON
          delivery.customer = customer.customer

          WHERE  AND
            date(delivery.timestamp) BETWEEN :strt AND :end
            AND customer.id = :id
          ORDER BY delivery.id ASC

This join will get you the results in one query with just one loop and probably save your database from getting hit with a large number of queries and bringing it it's knees. 这种联接将使您只需一个循环就可以在一个查询中获得结果,并且可以避免数据库被大量查询击中并使其屈膝。

Prepared statement 准备好的声明

The server can be brought to it's knees in another way, by SQL injection which your code currently allows. 服务器可以通过您的代码当前允许的SQL注入以另一种方式屈服。 That's why I have re written it to use place holders. 这就是为什么我将其重写为使用占位符的原因。 Now you need to prepare it and then execute it by passing parameters 现在您需要准备它,然后通过传递参数来执行它

$stmt = $db->prepare($sql);
$stmt->execute( array("id"=>$customer_id, "strt"=>$startdate, "end"=>$enddate));

Normalize 规范化

Not quite sure why you are storing customer which I assume to be customer name or customer email in the product table. 不太清楚您为什么要在产品表中存储customer (我假设是客户名称或客户电子邮件)。 You should be saving the customer id here to avoid redundancy and possibly faster queries. 您应该在此处保存客户ID,以避免冗余和可能更快的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM