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

I have after every row comma (,) but I don't want (,) after last row. How could I do this?

ccKep
  • 5,786
  • 19
  • 31
Lidia
  • 3
  • 1
  • 1
    These types of questions have been answered numerous times already on StackOverflow. Please [learn how to use the search function](https://stackoverflow.com/search?q=%5Bphp%5D+last+comma+loop). – Decent Dabbler Dec 04 '17 at 00:56

2 Answers2

0

Take a look at the join function:

$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;
ccKep
  • 5,786
  • 19
  • 31
  • I second that. I would've suggested `implode()`. I've never heard of the `join()` function. Is there any difference in performance between these 2 functions? – Ionut Tatu Dec 04 '17 at 00:54
  • @IonutTatu As the provided link to the docs state, it's just an alias to `implode()`. However, other languages (eg. .NET and JS) often call it `join` - so for consistency reason I always just remembered `join()` ;-) – ccKep Dec 04 '17 at 00:57
0

rtrim(yourstring,","), this function will remove right comma.