What are the advantages of using MySQLi or PDO over the traditional MySQL functions in PHP for database interactions?

When interacting with databases in PHP, using MySQLi or PDO over the traditional MySQL functions is advantageous because they offer more secure and flexible ways to connect to databases. MySQLi and PDO support prepared statements, which help prevent SQL injection attacks. Additionally, they provide object-oriented interfaces and support multiple database types, making it easier to switch databases in the future.

// Using MySQLi to connect to a database
$servername = "localhost";
$username = "root";
$password = "";
$database = "my_database";

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

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