What is the difference between mysqli and PDO in PHP?

The main difference between mysqli and PDO in PHP is that mysqli is specifically designed to work with MySQL databases, while PDO (PHP Data Objects) is a more flexible database access layer that can work with multiple database systems. PDO provides a consistent interface for accessing different types of databases, making it easier to switch between databases without changing your code.

// Example code using PDO to connect to a MySQL database
$dsn = 'mysql:host=localhost;dbname=mydatabase';
$username = 'username';
$password = 'password';

try {
    $pdo = new PDO($dsn, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected to database successfully";
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}