Douglas Douglas - 24 days ago 20
MySQL Question

Store html entities in database? Or convert when retrieved?

Quick question, is it a better idea to call

htmlentities()
(or
htmlspecialchars()
) before or after inserting data into the database?

Before: The new longer string will cause me to have to change the database to hold longer values in the field. (
maxlength="800"
could change to a 804 char string)

After: This will require a lot more server processing, and hundreds of calls to
htmlspecialchars()
could be made on every page load or AJAX load.

SOOO. Will converting when results are retrieved slow my code significantly? Should I change the DB?

Answer

I'd recommend storing the most raw form of the data in the database. That gives you the most flexibility when choosing how and where to output that data.

If you find that performance is a problem, you could cache the HTML-formatted version of this data somehow. Remember that premature optimization is a bad thing.

Comments