What best practices should be followed when assigning placeholders in a SQL query for user authentication?

When assigning placeholders in a SQL query for user authentication, it is important to use prepared statements to prevent SQL injection attacks. Prepared statements separate the SQL query from the user input, ensuring that the input is treated as data rather than executable code. This helps to protect the database from malicious input that could compromise its security.

// Assume $username and $password are user inputs

// Establish a database connection
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare a SQL query with placeholders
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username AND password = :password');

// Bind the user input to the placeholders
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $password);

// Execute the query
$stmt->execute();

// Fetch the result
$user = $stmt->fetch();