while循环php中的最后一个元素

时间:2017-12-04 00:47:15

标签: php loops while-loop

$result = $conn->query("SELECT * FROM mails");
while($row = $result->fetch_assoc()){
    $mymail= $row['mail']. ",";
    echo $mymail;
}

我在每行逗号(,)后都有,但我不想在最后一行之后(,)。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

查看join功能:

$mails = array();
$result = $conn->query("SELECT * FROM mails");
while($row = $result->fetch_assoc())
{
    $mails[] = $row['mail']; // Store E-Mail adresses in an array
}
$commaSeparatedMails = join(',', $mails); // Connect all array parts using a comma
echo $commaSeparatedMails;

答案 1 :(得分:0)

rtrim(yourstring,","),此函数将删除右逗号。