What are the differences between mysqli and mysql in PHP and why should one consider switching to mysqli?

The main differences between mysqli and mysql in PHP are that mysqli is an improved version of mysql with added features and better security measures. One should consider switching to mysqli because it supports prepared statements, transactions, and stored procedures, which can help prevent SQL injection attacks and improve overall database security.

// 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";