What are the potential pitfalls of using mysql_num_rows() to count records in PHP?

Using mysql_num_rows() to count records in PHP can be inefficient as it requires an additional query to fetch the count, which can impact performance in large datasets. It is also deprecated in newer versions of PHP and should be avoided in favor of using COUNT() directly in the SQL query. To solve this issue, you can modify your SQL query to include a COUNT() function to retrieve the count of records directly from the database.

// Connect to the database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Query to count records
$query = "SELECT COUNT(*) as totalRecords FROM your_table_name";
$result = mysqli_query($connection, $query);

// Fetch the count of records
$row = mysqli_fetch_assoc($result);
$totalRecords = $row['totalRecords'];

// Output the total count of records
echo "Total records: " . $totalRecords;

// Close the connection
mysqli_close($connection);