What are the steps to transition from using mysql_* functions to PDO in PHP for database interactions?

When transitioning from using mysql_* functions to PDO in PHP for database interactions, it is important to update your code to use PDO to ensure better security and compatibility with newer PHP versions. This involves replacing mysql_connect() with a PDO connection, updating query execution methods, and handling errors using PDO exceptions.

// Replace mysql_connect() with PDO connection
$pdo = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password');

// Update query execution methods
$stmt = $pdo->prepare('SELECT * FROM table_name WHERE id = :id');
$stmt->bindParam(':id', $id);
$stmt->execute();
$result = $stmt->fetchAll();

// Handle errors using PDO exceptions
try {
    $pdo = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}