Are there any security concerns related to directly using user input in SQL queries, as shown in the code example?

Directly using user input in SQL queries can lead to SQL injection attacks, where malicious users can manipulate the input to execute unauthorized SQL commands. To prevent this, it is recommended to use prepared statements with parameterized queries. This way, user input is treated as data rather than executable code, making the query safe from injection attacks.

// Using prepared statements to prevent SQL injection
$pdo = new PDO("mysql:host=localhost;dbname=myDB", "username", "password");

// User input
$userInput = $_POST['user_input'];

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

// Bind the user input to the placeholder
$stmt->bindParam(':username', $userInput, PDO::PARAM_STR);

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

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