MySQL 填充 Foreach

发布于 2024-12-20 18:35:51 字数 933 浏览 1 评论 0原文

我正在努力重建我不久前编写的新闻通讯系统。原始系统基于使用平面文件系统,我想将其转换为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕花眠 2024-12-27 18:35:51

您必须获取结果。您不能使用 foreach,您需要使用 while。

$handle = mysql_query("SELECT email FROM newsletter");    

while ($row = mysql_fetch_assoc( $handle ) )
{
    mail( $row['email'], $subject, $_POST['message'], $headers);
}

https://www.php.net/mysql_fetch_assoc

You have to fetch your results. You can't use foreach, you'll want to use while.

$handle = mysql_query("SELECT email FROM newsletter");    

while ($row = mysql_fetch_assoc( $handle ) )
{
    mail( $row['email'], $subject, $_POST['message'], $headers);
}

https://www.php.net/mysql_fetch_assoc

混吃等死 2024-12-27 18:35:51

该伪代码

$run = mysql_query("SELECT email FROM newsletter");    

foreach ($run as $value) { 
    mail($value, $subject, $_POST['message'], $headers);
}   

可以正确地重写为:

$result = mysql_query("SELECT body, emailaddress, subject  FROM emails");    
if (!$result) { die('error in mysql_query: '.mysql_error())}
while ($row = mysql_fetch_array($result) {
  $body = $row['body'];
  $address = $row['address'];
  $subject = $row['subject'];
  //do stuff with that data.
  //never use $_POST data directly, always feed it though `htmlentities` 
  //before outputting it (screen or email) 
  //or through mysql_real_escape_string() before inputting it in a DB.
  $echo htmlentities($body);  //sanitize your output or suffer XSS-attacks
  .....
}

如果您想使用一个 SELECT 的输出作为另一个 SELECT 的输入不要使用循环,而是使用连接:

SELECT e.body, e.subject, e.header, e.whatever FROM emails e
INNER JOIN newsletter n ON (n.id = e.newsletter_id)
WHERE n.id = 1466

This pseudo code

$run = mysql_query("SELECT email FROM newsletter");    

foreach ($run as $value) { 
    mail($value, $subject, $_POST['message'], $headers);
}   

Can be correctly rewritten as:

$result = mysql_query("SELECT body, emailaddress, subject  FROM emails");    
if (!$result) { die('error in mysql_query: '.mysql_error())}
while ($row = mysql_fetch_array($result) {
  $body = $row['body'];
  $address = $row['address'];
  $subject = $row['subject'];
  //do stuff with that data.
  //never use $_POST data directly, always feed it though `htmlentities` 
  //before outputting it (screen or email) 
  //or through mysql_real_escape_string() before inputting it in a DB.
  $echo htmlentities($body);  //sanitize your output or suffer XSS-attacks
  .....
}

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:

SELECT e.body, e.subject, e.header, e.whatever FROM emails e
INNER JOIN newsletter n ON (n.id = e.newsletter_id)
WHERE n.id = 1466
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文