How can I group rows with the same value from mySQL into php?
<p>This is my mySQL table <code>animals</code>:</p>

<pre><code>    ╔══════════╦══════╗
    ║  animal  ║ name ║
    ╠══════════╬══════╣
    ║   dog    ║ sam  ║
    ║   cat    ║ fred ║
    ║   cat    ║ todd ║
    ║  bird    ║ alan ║
    ╚══════════╩══════╝
</code></pre>

<p>I want to select all data into a table:</p>

<pre><code>            $sql = 'SELECT  *  FROM animals';
            foreach ($pdo-&gt;query($sql) as $row) {
                echo '&lt;td&gt;'.$row['animal'].' &lt;/td&gt;';
                echo '&lt;td&gt;'.$row['name'].' &lt;/td&gt;';
            }
</code></pre>

<p>My result is:</p>

<pre><code>    ╔══════════╦══════╗
    ║   dog    ║ sam  ║
    ║   cat    ║ fred ║
    ║   cat    ║ todd ║
    ║  bird    ║ alan ║
    ╚══════════╩══════╝
</code></pre>

<p>But I want to output rows with the same animal only once, but with all the names in one row, like this:</p>

<pre><code>    ╔══════════╦═════════════╗
    ║   dog    ║ sam         ║
    ║   cat    ║ fred, todd  ║
    ║  bird    ║ alan        ║
    ╚══════════╩═════════════╝
</code></pre>

<p>I have no idea how to achieve this. I try to think in this direction:</p>

<pre><code>SELECT  *  FROM animals GROUP BY animal
</code></pre>

<p>But I am stuck! I am happy for every hint!</p>