Parsing one col of multiple rows php

后端 未结 3 463
野的像风
野的像风 2021-01-26 03:35

I have e.g. the following data returned from a query, each name is an item:

id      name      comment
1       FF        hey
1       FF        hey back!
2       L         


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-01-26 04:02

    You can use GROUP_CONCAT() on your mysql query to group all the comments together for each name

    SELECT id, name
    GROUP_CONCAT(comment) AS comment
    FROM table
    GROUP BY name;
    

    then explode() the $row[comment] in your php code

    while ($row = mysql_fetch_array($result)){
    
    echo '
  • '.$row['name'].'
    '; if($row['comment'] != ""){ $comments = explode(",",$row['comment']); foreach($comments as $comment){ echo '
    '.$comment.'
    '; } } echo '
  • '; }

    Edit
    Thanks to @CBroe, I now know that GROUP_CONCAT() has a group_concat_max_len default of 1024. You will want to increase this before running the GROUP_CONCAT() query -

    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;
    

    you will also need to be aware of max_allowed_packet as this is the limit you can set var_group_concat_max_len to.

    note: mysql_query() does not allow multiple queries, so you will need to do 2 mysql_query(), and you can use SET SESSION ... so that all queries in your current session have that max_len. It would be better to change from mysql_ functions (which are depreciated) and change to mysqli_ or PDO as they offer multiple query option. also check out - http://php.net/manual/en/mysqlinfo.api.choosing.php

提交回复
热议问题