php - 将数组从 SQL 传递到 Swiftmailer

标签 php mysql arrays

我需要将包含电子邮件的数组传递给 Swiftmailer,以便它可以一次发送多封邮件。我应该传递给它的数组是: $to[];

我正在使用此代码从数据库获取数据,但当我 var_dump $to 时,它一直显示 array(0) { } 。数据库没有任何错误,我尝试使用 mysqli_error 但仍然没有任何错误:

                <?php
                $sql = "SELECT user_email,user_id FROM users";
                $q = mysqli_query($connection_offerme,$sql);
                if($q){
                    $to = array();
                    while($row = mysqli_fetch_assoc($q))
                    {
                        $to[] = $row['user_email'];
                    }

                    var_dump($to);
                }
                else {
                    echo "no results";
                    printf("Errormessage: %s\n", mysqli_error($connection_offerme));
                }

                ?>

编辑:PS。我从中得到的结果是 array(0) { } 而不是 "no results" 加上 sql 错误

最佳答案

答案很简单:PHP 不允许在 if(){}else{} 之间使用 var_dump(); 函数。

在上面的代码中正确使用 var_dump(); 是:

        <?php
        $sql = "SELECT user_email,user_id FROM users";
        $q = mysqli_query($connection_offerme,$sql);
        if($q){
            $to = array();
            while($row = mysqli_fetch_assoc($q))
            {
                $to[] = $row['user_email'];
            }

            var_dump($to); // - VAR_DUMP(); NOT ALLOWED HERE

        }
        else {
            echo "no results";
            printf("Errormessage: %s\n", mysqli_error($connection_offerme));
        }
            var_dump($to); // - VAR_DUMP(); SHOULD GO HERE INSTEAD
        ?>

关于php - 将数组从 SQL 传递到 Swiftmailer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38029028/

相关文章:

php - 无需 super 权限更改MySQL时区

php - "Temporary failure in name resolution": Symfony 5. 3 使用 Doctrine 和 Docker 连接到 MariaDB

mysql - 尝试刷新权限时 mysql 初始化文件中出现语法错误

python - numpy : indexes too big giving sometimes exceptions, 有时不是

ios - 将Firebase的数据库与Swift 4语言同步

php - 查询中两个时间戳的比较

php - 类 App\Http\Controllers\UserController 不存在

c++ - 二维数组中元素的地址

php - 如何?通过 jQuery.ajax :POST and using php: file_put_contents(); 从 CodeMirror 编辑器发送 PHP 代码

sql - 将查询限制为一条记录会提高性能吗