What are the potential pitfalls of using mysql_num_rows to count the number of rows in a MySQL result set in PHP?
Using `mysql_num_rows` to count the number of rows in a MySQL result set in PHP can be inefficient and may not work as expected in certain situations. It is recommended to use `mysqli_num_rows` or `PDOStatement::rowCount` instead, as they are more reliable and provide better performance.
// Connect to MySQL database using mysqli
$mysqli = new mysqli("localhost", "username", "password", "database");
// Execute query
$result = $mysqli->query("SELECT * FROM table");
// Check if query was successful
if($result) {
// Get number of rows
$num_rows = $result->num_rows;
// Output number of rows
echo "Number of rows: " . $num_rows;
} else {
// Handle query error
echo "Error executing query: " . $mysqli->error;
}
// Close connection
$mysqli->close();