How can input values be properly masked in PHP to prevent SQL injection attacks?

To prevent SQL injection attacks in PHP, input values can be properly masked by using prepared statements with parameterized queries. This technique separates SQL code from user input, preventing malicious SQL code from being executed. By binding parameters to placeholders in the SQL query, the input values are automatically sanitized, making it secure against SQL injection attacks.

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

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

// Bind the input value to the placeholder
$stmt->bindParam(':username', $_POST['username']);

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

// Fetch the results
$results = $stmt->fetchAll();