What are some best practices for transitioning from mysql_* functions to PDO in PHP?

When transitioning from mysql_* functions to PDO in PHP, it is important to update your code to use prepared statements to prevent SQL injection attacks and improve security. PDO provides a more flexible and secure way to interact with databases in PHP.

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

// Example query using prepared statement
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = :id');
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);