How can the GROUP BY clause affect the output of a PHP query and why is it important to use it correctly?

The GROUP BY clause in a SQL query is used to group rows that have the same values in specified columns. If the GROUP BY clause is not used correctly in a PHP query, it can affect the output by not grouping the data as intended, leading to incorrect results. It is important to use the GROUP BY clause correctly to ensure that the data is grouped and aggregated properly.

$query = "SELECT column1, SUM(column2) 
          FROM table_name 
          GROUP BY column1";
$result = mysqli_query($connection, $query);

if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
        echo $row['column1'] . ": " . $row['SUM(column2)'] . "<br>";
    }
}