What are the advantages of using mysqli or pdo over mysql in PHP for database operations?

Using mysqli or PDO over mysql in PHP for database operations offers several advantages, including improved security through the use of prepared statements, support for multiple database types, and better error handling capabilities. Additionally, mysqli and PDO are more modern and actively maintained compared to the deprecated mysql extension.

// Example using mysqli
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

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

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

// Perform database operations using mysqli

// Close connection
$conn->close();