PHP - 如何把查询结果在一个数组? [英] PHP - how to put query results in an array?

查看:202
本文介绍了PHP - 如何把查询结果在一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何把一个MySQL查询结果为可while循环外部访问数组。这可能吗?

我的测试code,我摆弄如下。我想将电子邮件发送到阵列中的电子邮件地址,而无需创建while循环中的电子邮件code。这样我可以注入数组结果进入BCC领域,这一次全部使用mail()函数,而不是打开每个电子邮件一个新的SMTP连接熄灭 - 还是让我觉得

 < PHP
如果(使用isset($ _ POST ['btnSendToSelected'])及和放大器;使用isset($ _ POST ['检查']))
{
    $检查= array_map('INTVAL',$ _ POST ['检查']);
    $ EMAIL_LIST =破灭(,$检查);    $ get_emails = mysqli_query($康恩,选择用户名,电子邮件用户的其中userid IN($ EMAIL_LIST))
    或死亡($ dataaccess_error);    而($行= mysqli_fetch_array($ get_emails))
    {
        $电子邮件=阵列($行[电子邮件]);
        $ emails_array =破灭(,,$电子邮件);
    }
        //这里发送电子邮件while循环外...
}
ELSEIF(使用isset($ _ POST ['btnSendToSelected'])及和放大器;!使用isset($ _ POST ['检查']))
{
    $味精= $ msg_error;
}
?>


解决方案

  $ emails_array =阵列();而($行= mysqli_fetch_array($ get_emails)){
    $ emails_array [] =爆炸(,$行[电子邮件]);
}的foreach($ emails_array为$值){
  mail_function($值);
}

或者

 而($行= mysqli_fetch_array($ get_emails)){
  的foreach(爆炸(,$行[电子邮件])作为$值){
    mail_function($值);
  }
}

I am trying to figure out how to put a mysql query result into an array that can be accessed outside the while loop. Is this possible?

My test code that I am playing with is below. I want to send email to the email addresses in the array without creating the email code inside the while loop. That way I can inject the array result into the BCC field and it all goes out at once using the mail() function rather than opening a new smtp connection for each email - or so I think.

<?php  
if(isset($_POST['btnSendToSelected']) && isset($_POST['checked']))
{
    $checked = array_map('intval',$_POST['checked']);
    $email_list = implode(", ", $checked);

    $get_emails = mysqli_query($conn, "SELECT UserName, Email FROM users WHERE UserId IN ($email_list)")
    or die($dataaccess_error);

    while($row = mysqli_fetch_array($get_emails))
    {
        $emails = array($row['Email']);
        $emails_array = implode(", ", $emails);
    }
        // send the email here outside the while loop...
}
elseif(isset($_POST['btnSendToSelected']) && !isset($_POST['checked']))
{
    $msg = $msg_error;
}
?>

解决方案

$emails_array = array();

while($row = mysqli_fetch_array($get_emails)) {
    $emails_array[] = explode(", ", $row['Email']);
}

foreach($emails_array as $value) {
  mail_function($value);
}

Or

while($row = mysqli_fetch_array($get_emails)) {
  foreach(explode(", ", $row['Email']) as $value) {
    mail_function($value);
  }
}

这篇关于PHP - 如何把查询结果在一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆