What are the advantages of using PDO over mysql_* in PHP for database operations?

Using PDO over mysql_* in PHP for database operations offers several advantages, including improved security through the use of prepared statements, support for multiple database drivers, and better error handling capabilities. PDO also provides a more consistent and object-oriented approach to working with databases in PHP.

// Using PDO for database operations
try {
    $pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");
    $stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id");
    $stmt->bindParam(':id', $id);
    $stmt->execute();
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    foreach ($result as $row) {
        // Process data
    }
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}