在连接两个表之后,我意识到有几行具有相同的电子邮件地址。我希望能够将共享同一电子邮件地址的所有行的内容邮寄到该电子邮件地址。即
餐桌食品
用户名|nbsp nbsp 电子邮件 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp 最喜爱的食物
12拉斐尔|12raphael@example.com nbsp nbsp 面条
米歇尔01 |michael01@example.com nbsp nbsp|龙虾
jordan606|jordan606@example.com nbsp 螃蟹
bryant001|bryant000@example.com nbsp nbsp|鸡肉
免费饮料
食品 nbsp nbsp |drink1 nbsp nbsp nbsp nbsp nbsp nbsp|drink2
面条|可乐 nbsp nbsp nbsp nbsp nbsp nbsp nbsp|矿泉水
龙虾|fanta nbsp nbsp nbsp nbsp nbsp nbsp nbsp|咖啡
面条|姜汁饮料 nbsp 柠檬饮料
鸡肉|姜汁啤酒 nbsp nbsp|精灵
$query=$conn->query("SELECT tablefood.username,
tablefood.email,tablefood.favoritefood,freedrinks.drink1,
freedrinks.drink2
FROM tablefood
INNER JOIN freedrinks
ON tablefood.favoritefood=freedrinks.food");
加入后,我们有这样的东西(例如)
12拉斐尔|12raphael@example.com nbsp nbsp 面条|可乐 nbsp nbsp nbsp nbsp nbsp nbsp nbsp|矿泉水
12拉斐尔|12raphael@example.com nbsp nbsp|面条|姜汁饮料 nbsp 柠檬饮料
操作数据和执行邮件操作,我有类似以下代码的东西。
if(!$query){
echo "Error! Can not query database";
die();
}
if($query->num_rows>0){
while($row = $query->fetch_assoc()){
$username=$row['username'];
$email=$row['email'];
$food=$row['favoritefood'];
$drink1=$row['drink1'];
$drink2=$row['drink2'];
$msg="Dear ".$username. "We are offering you one of the following
drinks for free ". $drink1." OR ".$drink2."rn";
mail("$email","Freedrink",$msg);
我的代码可以工作,但它只按加入的mySQL行发送邮件,即
亲爱的12拉斐尔,我们向您提供以下其中一种免费饮料可乐或矿泉水
亲爱的12拉斐尔,我们向您提供以下其中一种免费姜汁饮料或柠檬饮料
我想要的是这样:
亲爱的12拉斐尔,我们向您提供以下其中一种免费可乐或矿泉水饮料、姜汁饮料或柠檬饮料
到目前为止,我如何一次发送所有这些内容?各行都有相同的电子邮件地址。
这对我真的有很大帮助。感谢
if(!$query){
echo "Error! Can not query database";
die();
}
$drinks = '';
if($query->num_rows>0){
while($row = $query->fetch_assoc()){
if(empty($username)) {
$username=$row['username'];
$email=$row['email'];
$food=$row['favoritefood'];
}
$drink1=$row['drink1'];
$drink2=$row['drink2'];
if (!empty($drinks)) $drinks .=", ";
$drinks .= "$drink1 OR $drink2";
}
// after while cycle write this
$msg="Dear $username. We are offering you one of the following
drinks for free $drinksrn";
mail("$email","Freedrink",$msg);