What are the benefits of using the mysqli extension over the mysql extension in PHP?

The mysqli extension in PHP is preferred over the mysql extension because it offers improved security features, support for prepared statements, and enhanced functionality for working with MySQL databases. It also provides better performance and scalability compared to the older mysql extension, which is now deprecated.

// Connect to MySQL database using mysqli extension
$mysqli = new mysqli("localhost", "username", "password", "database");

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}