What are the advantages and disadvantages of using mysql_num_rows versus COUNT() in PHP for counting database entries?

When counting database entries in PHP, it is generally recommended to use the COUNT() function in SQL queries rather than the mysql_num_rows function. Using COUNT() directly in the query is more efficient as it performs the counting operation directly in the database server rather than fetching all the data to the PHP script and counting it there. This can lead to better performance, especially when dealing with large datasets. Additionally, using COUNT() provides more accurate results as it counts the actual number of rows in the database table, while mysql_num_rows may not always return the correct count due to buffering and caching issues.

// Using COUNT() in SQL query to count database entries
$query = "SELECT COUNT(*) as total FROM table_name";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
$count = $row['total'];
echo "Total entries: " . $count;