How can error handling be implemented in the code snippet provided to improve its robustness and reliability?

The code snippet provided does not have any error handling mechanisms in place, which can lead to unexpected behaviors or crashes if an error occurs during the execution of the script. To improve its robustness and reliability, error handling can be implemented using try-catch blocks to catch and handle any exceptions that may arise during the execution of the code.

try {
    $pdo = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $pdo->prepare('SELECT * FROM users WHERE id = :id');
    $stmt->bindParam(':id', $id, PDO::PARAM_INT);
    $stmt->execute();

    $user = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($user) {
        echo 'User found: ' . $user['name'];
    } else {
        echo 'User not found';
    }
} catch (PDOException $e) {
    echo 'Error: ' . $e->getMessage();
}