What are the advantages of using mysql_i or PDO over mysql in PHP?

Using mysqli or PDO over mysql in PHP is advantageous because they offer more secure and flexible ways to interact with databases. Both mysqli and PDO provide prepared statements, which help prevent SQL injection attacks. Additionally, they support multiple database types, making it easier to switch between different databases if needed. Lastly, mysqli and PDO are actively maintained and supported by the PHP community, while the mysql extension is deprecated in newer PHP versions.

// 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 the database';
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}