What are some best practices for handling SQL injections when working with databases in PHP?

SQL injections can be prevented by using parameterized queries or prepared statements in PHP when interacting with databases. This helps to sanitize user input and prevent malicious SQL code from being executed. Example PHP code snippet using prepared statements to prevent SQL injections:

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

// Prepare a SQL statement with a placeholder for user input
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');

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

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

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