What security measures should be implemented to prevent SQL injection when using user input in PHP queries?

To prevent SQL injection when using user input in PHP queries, you should use prepared statements with parameterized queries. This helps to separate the SQL query logic from the user input, preventing malicious SQL code from being executed.

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

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

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

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