What is the purpose of using bindParam in PDO and what are the potential pitfalls when not specifying the parameter type?

When using bindParam in PDO, it is important to specify the parameter type to ensure proper data binding and prevent SQL injection attacks. If the parameter type is not specified, PDO may not handle the data correctly, leading to potential security vulnerabilities and unexpected behavior in queries.

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