如何过滤while循环中的项目?
How to filter items in while loop?
SQLTable[订单]
| orderId | dueDate | emailAddress |
| ------- | ---------- | -------------- |
| 1010101 | 10/11/2021 | joe@gmail.com |
| 1010102 | 10/11/2021 | joe@gmail.com |
| 1010103 | 10/11/2021 | joe@gmail.com |
| 1010104 | 10/11/2021 | john@gmail.com |
| 1010105 | 10/11/2021 | john@gmail.com |
| 1010106 | 10/11/2021 | john@gmail.com |
PHP 脚本
$query = "SELECT * FROM orders";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result)) {
$order = $row['orderId'];
$to = $row['emailAddress'];
$sub = "Payment Due Reminder";
$body = "Due reminder message with order ID $order";
mail($to, $sub, $body);
}
我的要求
现在我只想发送一封列出三个订单 ID 的电子邮件,而不是向同一收件人发送三封电子邮件。有办法实现吗?
我会在 MySQL 内使用聚合查询处理此问题:
$query = "SELECT dueDate, emailAddress, GROUP_CONCAT(orderId) AS all_orders
FROM orders
GROUP BY dueDate, emailAddress";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result)) {
$all_orders = $row['all_orders'];
$to = $row['emailAddress'];
$sub = "Payment Due Reminder";
$body = "Due reminder message with order ID $all_orders";
mail($to, $sub, $body);
}
SQLTable[订单]
| orderId | dueDate | emailAddress |
| ------- | ---------- | -------------- |
| 1010101 | 10/11/2021 | joe@gmail.com |
| 1010102 | 10/11/2021 | joe@gmail.com |
| 1010103 | 10/11/2021 | joe@gmail.com |
| 1010104 | 10/11/2021 | john@gmail.com |
| 1010105 | 10/11/2021 | john@gmail.com |
| 1010106 | 10/11/2021 | john@gmail.com |
PHP 脚本
$query = "SELECT * FROM orders";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result)) {
$order = $row['orderId'];
$to = $row['emailAddress'];
$sub = "Payment Due Reminder";
$body = "Due reminder message with order ID $order";
mail($to, $sub, $body);
}
我的要求
现在我只想发送一封列出三个订单 ID 的电子邮件,而不是向同一收件人发送三封电子邮件。有办法实现吗?
我会在 MySQL 内使用聚合查询处理此问题:
$query = "SELECT dueDate, emailAddress, GROUP_CONCAT(orderId) AS all_orders
FROM orders
GROUP BY dueDate, emailAddress";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result)) {
$all_orders = $row['all_orders'];
$to = $row['emailAddress'];
$sub = "Payment Due Reminder";
$body = "Due reminder message with order ID $all_orders";
mail($to, $sub, $body);
}