What are the potential pitfalls of using mysql_num_rows() to count the number of records in a table?

Using mysql_num_rows() to count the number of records in a table can be inefficient for large datasets as it retrieves all rows from the database to count them. It is recommended to use SQL queries with COUNT() function to directly retrieve the count of rows without fetching all data.

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

// Query to count number of rows
$query = "SELECT COUNT(*) as count FROM table_name";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);

// Get the count of rows
$count = $row['count'];

// Use the count as needed
echo "Number of rows: " . $count;

// Close connection
mysqli_close($conn);