In PHP and MySQL integration, what are the common pitfalls to avoid when querying and displaying data from a database?

One common pitfall to avoid when querying and displaying data from a database is not properly sanitizing user input, which can lead to SQL injection attacks. To prevent this, always use prepared statements or parameterized queries when interacting with the database.

// Example of querying database with prepared statement to avoid SQL injection

// Assuming $db is a PDO object connected to the database

$user_input = $_POST['user_input']; // User input that needs to be sanitized

$stmt = $db->prepare("SELECT * FROM users WHERE username = :username");
$stmt->bindParam(':username', $user_input);
$stmt->execute();

while ($row = $stmt->fetch()) {
    // Display data from the database
    echo $row['username'] . "<br>";
}