The best way to prevent any SQL injection in PHP is to use prepared statements with parametrized queries. This technique prevents malicious inputs from being executed as SQL code.
Here's an example on how SQL queries can be handles securely using PDO (PHP Data Objects):
<?php
// Establish a connection
$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->bindParam(':username', $user_input);
$stmt->execute();
$results = $stmt->fetchAll();
?>
In this example, the :username placeholder is ensuring that the input is treated as data and not as a executable code.