Are there any potential pitfalls or security risks associated with using user input directly in a PHP query like in the provided code snippet?

Using user input directly in a PHP query can lead to SQL injection attacks, where malicious users can manipulate the query to access or modify data in unintended ways. To prevent this, it's important to sanitize and validate user input before using it in a query. One way to do this is by using prepared statements with parameterized queries, which separate the SQL query logic from the user input data.

// Sanitize and validate user input
$user_input = $_POST['user_input'];
$user_input = filter_var($user_input, FILTER_SANITIZE_STRING);

// Prepare a parameterized query using prepared statements
$stmt = $pdo->prepare("SELECT * FROM table_name WHERE column_name = :user_input");
$stmt->bindParam(':user_input', $user_input);
$stmt->execute();

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

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