Quick question, is it a better idea to call htmlentities() (or htmlspecialchars()) before or after inserting data into the database?
It's best to store text as raw and encode it as needed, to be honest, you always need to htmlencode your data anyways when you're outputting it to the wbe page to prevent XSS hacking.
You shouldn't encode your data before you put it in the database. The main reason are: