What alternative function can be used in place of mysql_num_rows for better performance and security?

Using the mysqli_num_rows function instead of mysql_num_rows can provide better performance and security. The mysqli extension is more secure and supports prepared statements, which help prevent SQL injection attacks. Additionally, mysqli_num_rows is specifically designed for use with the mysqli extension, making it a more suitable choice for modern PHP applications.

// Connect to the database using mysqli
$mysqli = new mysqli('localhost', 'username', 'password', 'database');

// Check for connection errors
if ($mysqli->connect_error) {
    die('Connection failed: ' . $mysqli->connect_error);
}

// Execute a query
$result = $mysqli->query("SELECT * FROM table");

// Get the number of rows in the result set
$num_rows = $result->num_rows;

// Output the number of rows
echo "Number of rows: " . $num_rows;

// Close the connection
$mysqli->close();