What are common pitfalls when trying to display entries from a MySQL database using PHP?

One common pitfall when displaying entries from a MySQL database using PHP 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. Additionally, ensure that you are handling errors properly to avoid displaying sensitive information to users.

// Establish a connection to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Use prepared statements to retrieve and display entries from the database
$stmt = $conn->prepare("SELECT id, name, email FROM users");
$stmt->execute();
$stmt->bind_result($id, $name, $email);

while ($stmt->fetch()) {
    echo "ID: " . $id . " | Name: " . $name . " | Email: " . $email . "<br>";
}

$stmt->close();
$conn->close();