What are the potential risks of using $_GET variables directly in SQL queries in PHP?

Using $_GET variables directly in SQL queries can make your application vulnerable to SQL injection attacks, where an attacker can manipulate the input to execute malicious SQL queries. To prevent this, you should always sanitize and validate user input before using it in SQL queries. One way to do this is by using prepared statements with parameterized queries in PHP.

// Sanitize and validate the input before using it in a SQL query
$user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : 0;

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

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