How can PHP developers securely handle user input from GET variables to prevent vulnerabilities like SQL injection?

To securely handle user input from GET variables and prevent vulnerabilities like SQL injection, PHP developers should always sanitize and validate the input before using it in database queries. This can be done by using prepared statements with parameterized queries to separate the SQL query logic from the user input data, ensuring that malicious input does not interfere with the query execution.

// Example of securely handling user input from GET variables
$user_input = $_GET['user_input'];

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

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

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

// Use the results as needed
foreach ($results as $row) {
    // Process the data
}