Get all members from each group
$sql = &lt;&lt;&lt;'EOSQL'
SELECT g.name, g.year, GROUP_CONCAT(m.name SEPARATOR ', ') AS members
FROM groups g INNER JOIN members m on g.id = m.idGroup
GROUP BY m.idGroup
EOSQL;


The key insight here is that GROUP_CONCAT and GROUP are what you need to combine multiple rows of the result into one.  Note that GROUP is a SQL name.  It is just coincidence that you also have a table named groups.  

I also used a Nowdoc because the SQL string is using multiple lines.  I picked Nowdoc because we aren't doing any interpolation or other escaping in the string.  

It's obsolete now (assuming you use the INNER JOIN version), but in 


  while($member =   $ergebnis-&gt;fetch_assoc())
  {
    if(!empty($members)) $members .= ", ";
    $members .= htmlspecialchars($member["name"]);
  }



It looks like you forgot to change a variable name.  I think that $ergebnis should be $memberResult.  But the main point I wanted to make is that at the expense of some mildly duplicate code, you don't need to check every iteration to see if it is the first.  

  if ($member = $memberResult-&gt;fetch_assoc())
  {
    $members = htmlspecialchars($member['name']);

    while ($member = $memberResult-&gt;fetch_assoc())
    {
      $members .= ', ' . htmlspecialchars($member['name']);
    }
  }


On the first row, you just set the variable.  On subsequent rows, you append the separator and the new value to the variable.  This produces the same result without all the checks to see if you are on the first iteration.  

I find it more readable to put a space between while and the opening parenthesis.  That makes it easier to see that we are not making a function call.  The additional whitespace also breaks up the line and makes it easier for a human to parse (the compiler doesn't care either way).  

In PHP, a double-quoted string allows variable interpolation.  So if I'm not using variable interpolation, I use a single-quoted string.  There's a slight performance benefit.  But the real advantage is that when I see a single-quoted string, I know that I don't have to look for variables being interpolated.  


  ?&gt;



I find it more readable to always put the ?&gt; at the far left (no indent) when the PHP block spans multiple lines.  Perhaps that is just me, but I find it easier to scan for ?&gt; if they are always in the same place.