What potential pitfalls should be considered when ordering and displaying data in PHP?

One potential pitfall when ordering and displaying data in PHP is not properly sanitizing user input, which can lead to SQL injection attacks. To prevent this, always use prepared statements when querying the database to ensure that user input is not directly inserted into SQL queries.

// Example of using prepared statements to prevent SQL injection
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();
$results = $stmt->fetchAll();
foreach ($results as $row) {
    echo $row['username'] . "<br>";
}