There is a sign news . there are columns id , category , text

 $res = mysqli_query($link, "SELECT * FROM `news` GROUP BY `category` ORDER BY `category` DESC"); while($row = mysqli_fetch_assoc($res){ echo $row['category']; echo $row['text']; } 

category is displayed, text is displayed, but if there is more than one in this category, the query displays only one text .

What am I doing wrong?

I need to display the category and text all articles that correspond to this category, without duplicating the category.

    1 answer 1

    Use GROUP_CONCAT :

      SELECT category, GROUP_CONCAT(`text` SEPARATOR ', ') as `text` FROM `news` GROUP BY `category` ORDER BY `category` DESC" 

    http://sqlfiddle.com/#!9/ea41f/189

    However, remember that GROUP_CONCAT has a maximum length limit of 1024 bytes (by default).

    • and how to display text not through a comma, but from a new paragraph? - Yuri
    • one
      After SEPARATOR in quotes, you can specify anything. For example, SEPARATOR '<br>' - Crantisz
    • and how to be for example with pictures? echo '<img src = "resource /'.$ row [' text '].'"> after all, it will duplicate the entire output to the address, instead of displaying other pictures, how to be? - Yuri