MySQL 填充 Foreach
我正在努力重建我不久前编写的新闻通讯系统。原始系统基于使用平面文件系统,我想将其转换为 MySQL。我剩下的就是重建最终功能。我的问题是我需要做的是对数据库中存储的每封电子邮件使用循环。我认为each循环是最好的方法,但是如何一起使用MySQL和foreach呢?这是我当前的代码:
$run = mysql_query("SELECT email FROM newsletter");
foreach ($run as $value) {
mail($value, $subject, $_POST['message'], $headers);
}
我尝试了很多不同的方法,但似乎没有任何效果。我什至尝试过这样的事情:
$run = mysql_query("SELECT email FROM newsletter");
$email = mysql_fetch_array($run, MYSQL_ASSOC)
$cols = implode (', ', $email);
$run2 = mysql_query("SELECT $cols FROM newsletter");
while($emaillist = mysql_fetch_array($run2, MYSQL_ASSOC)){
foreach ($emaillist as $value) {
mail($value, $subject, $_POST['message'], $headers);
}
}
那是基于我看来的其他一些例子。它不会给出任何错误,但不会发送电子邮件。我将非常感谢一些帮助。
I am working on rebuilding a newsletter system I wrote a while ago. The original system was based around using a flat file system, and I want to convert it over to MySQL. All I have left is to rebuild the end function. My problem is that I need to do is use a loop for every email stored in the database. I figured a each loop is the best way to do this, but how do you use MySQL and foreach together? Here's my current code:
$run = mysql_query("SELECT email FROM newsletter");
foreach ($run as $value) {
mail($value, $subject, $_POST['message'], $headers);
}
I've tried a lot of different things and nothing seems to work. I've even tried something like this:
$run = mysql_query("SELECT email FROM newsletter");
$email = mysql_fetch_array($run, MYSQL_ASSOC)
$cols = implode (', ', $email);
$run2 = mysql_query("SELECT $cols FROM newsletter");
while($emaillist = mysql_fetch_array($run2, MYSQL_ASSOC)){
foreach ($emaillist as $value) {
mail($value, $subject, $_POST['message'], $headers);
}
}
That was based off of a few other examples i seem. it doesnt give any errors, but it doesnt send the email. I would greatly appreciate some help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须获取结果。您不能使用 foreach,您需要使用 while。
https://www.php.net/mysql_fetch_assoc
You have to fetch your results. You can't use foreach, you'll want to use while.
https://www.php.net/mysql_fetch_assoc
该伪代码
可以正确地重写为:
如果您想使用一个 SELECT 的输出作为另一个 SELECT 的输入不要使用循环,而是使用连接:
This pseudo code
Can be correctly rewritten as:
If you want to use the output of one SELECT as the input of another select do not use a loop but use a join instead: