What are some best practices for securing user input when using PDO?

When using PDO in PHP to interact with a database, it is important to properly secure user input to prevent SQL injection attacks. One of the best practices for securing user input is to use prepared statements with placeholders instead of directly interpolating user input into the SQL query.

// Example of using prepared statements with PDO to secure user input
// Assuming $pdo is your PDO connection object

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

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

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

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

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