What are the advantages of using PDO or MySQLi over the deprecated MySQL interface in PHP for database interactions?
The advantages of using PDO or MySQLi over the deprecated MySQL interface in PHP for database interactions include improved security through the use of prepared statements, support for multiple database systems, and object-oriented approach for easier maintenance and scalability.
// Using PDO to connect to a MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}