I have a column (named user_email) in a table called (event) in my database, This column has email addresses. I want to get all these email addresses and send one email for all of them. I tried to use 'for'loop but it only sends the email to one email only, I might be using for in a wrong way, any suggestion,
我的数据库中有一个名为(event)的表中有一个列(名为user_email),此列包含电子邮件地址。我想获取所有这些电子邮件地址,并为所有这些地址发送一封电子邮件。我尝试使用'for'loop但它只将电子邮件发送到一封电子邮件,我可能会以错误的方式使用,任何建议,
Part of my code
我的部分代码
$select="SELECT * FROM event WHERE event_title='$title'";
$query_select= mysql_query($select);
$row = mysql_fetch_array($result);
$email_to=$row["user_email"];
for($x=0;$x<count($row['user_email']);$x++){
@mail($email_to,$email_subject,$email_message,$header);
}
2 个解决方案
#1
You made it waaaayyy more complicated than it needs to be. Just loop through your results and send your email:
你让它变得比它需要的更复杂。只需循环搜索结果并发送电子邮件:
$select="SELECT * FROM event WHERE event_title='$title'";
$result = mysql_query($select);
while ($row = mysql_fetch_array($result)) {
mail($row['user_email'],$email_subject,$email_message,$header);
}
You really need to improve upon this as you don't check for mail errors (and even hide any PHP is trying to tell you about).
你真的需要改进这一点,因为你没有检查邮件错误(甚至隐藏任何PHP试图告诉你)。
Please, don't use mysql_*
functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
请不要在新代码中使用mysql_ *函数。它们不再维护,并且已被正式弃用。看到红色的盒子?了解准备好的语句,并使用PDO或MySQLi - 本文将帮助您确定哪些。如果您选择PDO,这是一个很好的教程。
#2
You need a while
loop. This way it iterates over all the rows in your result set. I had to guess at your column names so you'll need to fix this most likely
你需要一个while循环。这样,它会迭代结果集中的所有行。我不得不猜测你的列名,所以你最需要解决这个问题
while($row = mysql_fetch_assoc($query_select)) {
@mail($row['email_to'],$row['email_subject'],$row['email_message'],$header);
}
#1
You made it waaaayyy more complicated than it needs to be. Just loop through your results and send your email:
你让它变得比它需要的更复杂。只需循环搜索结果并发送电子邮件:
$select="SELECT * FROM event WHERE event_title='$title'";
$result = mysql_query($select);
while ($row = mysql_fetch_array($result)) {
mail($row['user_email'],$email_subject,$email_message,$header);
}
You really need to improve upon this as you don't check for mail errors (and even hide any PHP is trying to tell you about).
你真的需要改进这一点,因为你没有检查邮件错误(甚至隐藏任何PHP试图告诉你)。
Please, don't use mysql_*
functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
请不要在新代码中使用mysql_ *函数。它们不再维护,并且已被正式弃用。看到红色的盒子?了解准备好的语句,并使用PDO或MySQLi - 本文将帮助您确定哪些。如果您选择PDO,这是一个很好的教程。
#2
You need a while
loop. This way it iterates over all the rows in your result set. I had to guess at your column names so you'll need to fix this most likely
你需要一个while循环。这样,它会迭代结果集中的所有行。我不得不猜测你的列名,所以你最需要解决这个问题
while($row = mysql_fetch_assoc($query_select)) {
@mail($row['email_to'],$row['email_subject'],$row['email_message'],$header);
}