What security considerations should be taken into account when directly passing $_GET variables to SQL queries in PHP scripts?

When directly passing $_GET variables to SQL queries in PHP scripts, it is important to sanitize and validate the input to prevent SQL injection attacks. This can be done by using prepared statements with parameterized queries to ensure that user input is treated as data rather than executable code.

// Sanitize and validate the $_GET input
$user_input = filter_input(INPUT_GET, 'user_input', FILTER_SANITIZE_STRING);

// Prepare a SQL statement using a parameterized query
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
$stmt->bindParam(':username', $user_input, PDO::PARAM_STR);
$stmt->execute();

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

// Loop through the results
foreach ($results as $row) {
    // Process the data
}