Should we HTML-encode special characters before storing them in the database?

Don't HTML-encode your characters before storage. You should store as pure a form of your data as possible. HTML encoding is needed because you are going to display the data on an HTML page, so do the encoding during the processing of the data to create the page. For example, suppose you decide you're also going to send the data in plain text emails. If you've HTML-encoded the data, now the HTML encoding is a barrier that you have to undo.

Choose a canonical form for your data, and store that. UTF-8 is wonderful, and your database supports it (assuming you've created all your tables properly). Just store UTF-8.


Going by the purpose of Database, its not advisable to HTML encode and store the data. Doing so will make the data desirable only for rendering on HTML pages(the one purpose) and for all other operations(many) you need to again decode. This degrades data consistency(since validity, accuracy, usability are hampered) property of Database.