What is the significance of the "group by" clause in a SQL query when using the "Count" function in PHP?

When using the "Count" function in PHP to retrieve the number of rows in a database table, it is important to use the "group by" clause in the SQL query to group the results by a specific column. This ensures that the count is calculated based on distinct values in that column, rather than simply counting all rows in the table. Without the "group by" clause, the count may not accurately reflect the desired information, especially when dealing with grouped data.

// SQL query with "group by" clause to count rows based on a specific column
$sql = "SELECT column_name, COUNT(*) as count FROM table_name GROUP BY column_name";
$result = mysqli_query($connection, $sql);

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo "Count for ".$row['column_name'].": ".$row['count']."<br>";
    }
} else {
    echo "No results found.";
}