What best practices should be followed when writing PHP scripts to handle database operations, especially when it involves sensitive user data?

When handling sensitive user data in PHP scripts for database operations, it is crucial to follow best practices for security. This includes using parameterized queries to prevent SQL injection attacks, validating and sanitizing user input, encrypting sensitive data before storing it in the database, and implementing proper error handling to prevent leaking sensitive information.

// Example of using parameterized queries to handle database operations securely
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

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

// Bind values to placeholders
$stmt->bindParam(':username', $_POST['username']);

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

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