What are the common pitfalls when trying to display data from a database in PHP?

One common pitfall when displaying data from a database in PHP is not properly sanitizing input, which can lead to SQL injection attacks. To solve this issue, always use prepared statements or parameterized queries to securely interact with the database.

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

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

// Bind parameters
$stmt->bindParam(':value', $value);

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

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

// Display the data
foreach ($results as $row) {
    echo $row['column_name'] . "<br>";
}