What potential pitfalls should be avoided when using PHP to handle database queries and display results?

One potential pitfall to avoid when using PHP to handle database queries and display results is SQL injection. This can occur when user input is directly inserted into SQL queries without proper sanitization, allowing malicious users to execute arbitrary SQL statements. To prevent SQL injection, always use prepared statements with parameterized queries to securely handle user input.

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

// Prepare a statement with a parameterized query
$stmt = $pdo->prepare('SELECT * FROM table WHERE column = :value');

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

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

// Display the results
while ($row = $stmt->fetch()) {
    echo $row['column'];
}