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

The main difference between using the mysql extension and mysqli extension in PHP for database connections is that the mysql extension is deprecated as of PHP 5.5.0 and removed in PHP 7.0.0, while the mysqli extension is the improved version that supports MySQL 4.1.3 or newer. It is recommended to use the mysqli extension for better security, performance, and functionality.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

// Using mysqli extension for database connection
$conn = new mysqli($servername, $username, $password, $dbname);

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