What are some best practices for sorting and counting data in PHP MySQL queries?

When sorting and counting data in PHP MySQL queries, it is important to use the ORDER BY clause to sort the results according to a specific column, and the COUNT() function to count the number of rows returned by the query. Additionally, using the GROUP BY clause can help in counting data based on a specific column value.

// Sorting data in ascending order based on a specific column
$query = "SELECT * FROM table_name ORDER BY column_name ASC";
$result = mysqli_query($connection, $query);

// Counting the number of rows returned by the query
$query = "SELECT COUNT(*) as total_rows FROM table_name";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
$total_rows = $row['total_rows'];

// Counting data based on a specific column value
$query = "SELECT column_name, COUNT(*) as count FROM table_name GROUP BY column_name";
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)) {
    echo $row['column_name'] . ": " . $row['count'] . "<br>";
}