In what ways can the provided PHP code snippet be improved for better functionality and error handling?

The provided PHP code snippet lacks proper error handling, which can lead to unexpected behavior or security vulnerabilities. To improve functionality and error handling, we can add try-catch blocks to catch and handle any exceptions that may occur during the execution of the code.

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

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

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

    if ($user) {
        // Process user data
    } else {
        throw new Exception("User not found");
    }
} catch (PDOException $e) {
    echo "Database error: " . $e->getMessage();
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}