What are some common pitfalls when displaying data in tabular form using PHP and MySQL?

One common pitfall when displaying data in tabular form using PHP and MySQL 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 properly escaped.

// Connect to the database
$mysqli = new mysqli("localhost", "username", "password", "database");

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

// Bind parameters
$stmt->bind_param("s", $user_input);

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

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

// Display the data in a table
echo "<table>";
while ($row = $result->fetch_assoc()) {
    echo "<tr>";
    echo "<td>" . $row['column1'] . "</td>";
    echo "<td>" . $row['column2'] . "</td>";
    // Add more columns as needed
    echo "</tr>";
}
echo "</table>";

// Close the statement and connection
$stmt->close();
$mysqli->close();