Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use <a href="http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat" rel="nofollow"><code>GROUP_CONCAT()</code></a> on your mysql query to group all the comments together for each name </p> <pre><code>SELECT id, name GROUP_CONCAT(comment) AS comment FROM table GROUP BY name; </code></pre> <p>then <a href="http://www.php.net/manual/en/function.explode.php" rel="nofollow"><code>explode()</code></a> the <code>$row[comment]</code> in your php code</p> <pre><code>while ($row = mysql_fetch_array($result)){ echo '&lt;li&gt;&lt;div class=className&gt;&lt;div class=itemName&gt;'.$row['name'].'&lt;/div&gt;'; if($row['comment'] != ""){ $comments = explode(",",$row['comment']); foreach($comments as $comment){ echo '&lt;div class=newRow&gt;'.$comment.'&lt;/div&gt;'; } } echo '&lt;/div&gt;&lt;/li&gt;'; } </code></pre> <p><strong>Edit</strong><br> Thanks to @CBroe, I now know that <code>GROUP_CONCAT()</code> has a <a href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_group_concat_max_len" rel="nofollow"><code>group_concat_max_len</code></a> default of 1024. You will want to increase this before running the <code>GROUP_CONCAT()</code> query - </p> <pre><code>SET [GLOBAL | SESSION] group_concat_max_len = 10240; // must be in multiples of 1024 SELECT id, name GROUP_CONCAT(comment) AS comment FROM table GROUP BY name; </code></pre> <p>you will also need to be aware of <a href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_max_allowed_packet" rel="nofollow"><code>max_allowed_packet</code></a> as this is the limit you can set <code>var_group_concat_max_len</code> to. </p> <p>note: <code>mysql_query()</code> does not allow multiple queries, so you will need to do 2 <code>mysql_query()</code>, and you can use <code>SET SESSION ...</code> so that all queries in your current session have that <code>max_len</code>. It would be better to change from <code>mysql_</code> functions (which are depreciated) and change to <a href="http://www.php.net/manual/en/book.mysqli.php" rel="nofollow"><code>mysqli_</code></a> or <a href="http://www.php.net/manual/en/ref.pdo-mysql.php" rel="nofollow"><code>PDO</code></a> as they offer multiple query option. also check out - <a href="http://php.net/manual/en/mysqlinfo.api.choosing.php" rel="nofollow">http://php.net/manual/en/mysqlinfo.api.choosing.php</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload