What are some potential pitfalls of using htmlentities() in PHP when interacting with a database?

Using htmlentities() on data before storing it in a database can lead to encoding issues when retrieving and displaying the data later. It is recommended to only use htmlentities() when outputting data to prevent XSS attacks, rather than when storing data in the database.

// Storing data in the database without using htmlentities()
$data = "This is <b>bold</b> text";
$sql = "INSERT INTO table_name (column_name) VALUES ('$data')";
// Execute the SQL query
```

```php
// Retrieving and displaying data with htmlentities() to prevent XSS attacks
$sql = "SELECT * FROM table_name";
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_assoc($result)) {
    echo htmlentities($row['column_name']);
}