Is using the LIKE operator in a SQL query for username validation a recommended practice in PHP?

When validating a username in PHP using a SQL query, using the LIKE operator can be risky as it can open up the possibility of SQL injection attacks. It is recommended to use parameterized queries with placeholders to safely validate usernames. This helps prevent malicious users from manipulating the SQL query.

$username = $_POST['username'];

// Prepare a SQL statement with a placeholder for the username
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();

// Check if a row was returned
if($stmt->rowCount() > 0) {
    // Username is valid
    echo "Username is valid.";
} else {
    // Username is not valid
    echo "Username is not valid.";
}