How does using COUNT() in MySQL compare to using mysql_num_rows() in PHP in terms of efficiency?

Using COUNT() in MySQL is generally more efficient than using mysql_num_rows() in PHP because the former directly retrieves the count of rows from the database server, while the latter requires fetching all rows and then counting them in PHP. Therefore, using COUNT() in MySQL can reduce the amount of data transferred between the database server and the PHP script, resulting in better performance.

// Using COUNT() in MySQL to get the number of rows
$query = "SELECT COUNT(*) FROM table_name";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_array($result);
$count = $row[0];

echo "Number of rows: " . $count;