How can PDO Prepared Statements be properly formatted in PHP for SQL queries?

When using PDO Prepared Statements in PHP for SQL queries, it is important to properly format the query with placeholders for the variables that will be bound later. This helps prevent SQL injection attacks and ensures the query is executed safely. To format a PDO Prepared Statement in PHP, you can use a query with placeholders and then bind the variables before executing the query.

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

// Bind the variables to the placeholders
$stmt->bindParam(':username', $username, PDO::PARAM_STR);

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