What are the advantages of using mysql_num_rows() over a while loop when checking for existing data in a database query result?

When checking for existing data in a database query result, using mysql_num_rows() is more efficient than using a while loop. mysql_num_rows() directly returns the number of rows in the result set, allowing you to quickly determine if there is any data without having to iterate through each row. This can improve performance and reduce unnecessary processing when simply checking for the existence of data.

// Using mysql_num_rows() to check for existing data
$result = mysql_query("SELECT * FROM table WHERE condition");
if(mysql_num_rows($result) > 0) {
    // Data exists
    // Proceed with your logic
} else {
    // No data found
}