简体   繁体   English

PHP:创建一个用逗号分隔的列表

[英]PHP: create a list separated by comma

I am trying to create a list of emails separated by coma from a prepared statement. 我正在尝试从准备好的语句中创建一个用逗号分隔的电子邮件列表。

                $prep_stmt = "SELECT m.email
                                FROM roster_par_membre rm
                                LEFT JOIN membre m 
                                        ON m.id_membre = rm.id_membre                           
                                WHERE id_roster = ? 
                                ";
                $stmt = $mysqli->prepare($prep_stmt);           
                if ($stmt) {
                    $stmt->bind_param('i', $roster_id);
                    $stmt->execute();
                    $stmt->store_result();
                    // get variables from result.
                    $stmt->bind_result($emails_roster);
                    $list = array(); (added)
                    while ($stmt->fetch()){
                        $list[] = $emails_roster;
                    }   
                    $list = implode(',', $list);

The output should be a list of emails without separations. 输出应为不带分隔符的电子邮件列表。 How can I get a list with coma that I can directly use to send an email to this list? 如何获得带有昏迷的列表,我可以直接使用该列表向该列表发送电子邮件? At the moment I get a fatal Error: Fatal error: [] operator not supported for strings (for the line inside the while! 此刻我出现了致命错误:致命错误:[]字符串不支持[]运算符(while内的行!

Try implode : 尝试implode

while ($stmt->fetch()){
   $emails[] = $emails_roster;
}
$emails = implode(',', $emails);

Not sure if I understood it correctly, but if your $emails_roster contains an email then simply append to an array: 不知道我是否理解正确,但是如果您的$emails_roster包含电子邮件,则只需追加到数组即可:

$emails = [];
while ($stmt->fetch()){
 $emails[] = $emails_roster;
}   

print_r($emails); 
echo implode(',', $emails);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM