What are the best practices for securing user input in PHP to prevent SQL Injections?

To prevent SQL Injections in PHP, it is important to use prepared statements with parameterized queries when interacting with a database. This helps sanitize user input and prevents malicious SQL queries from being executed.

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

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

// Bind the user input to the parameter
$stmt->bindParam(':username', $_POST['username']);

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

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