What are the differences between using mysql and mysqli in PHP for database connections?

When connecting to a MySQL database in PHP, it is recommended to use the mysqli extension over the older mysql extension. The mysqli extension offers improved security features, support for prepared statements, and better overall performance. It is also actively maintained and supported by the PHP community, unlike the deprecated mysql extension.

// Using mysqli to connect to a MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";