Ideally, you will never have to hide anything before using it in a query using prepared PDO instructions. Base libraries will take care of you.
In practice, if you cannot / will not use prepared instructions, escaping should only be done immediately before building the query string. Do not blindly go and reassign the contents of various superglobals (GET, POST, REQUEST, COOKIES), based on the assumption that everything will be included in the database. Think about when you should first check the form data, and some fields are filled out incorrectly. Now you need to cancel everything from the "database mode" and return to the "html mode" in order to insert the good data back into the form.
The same goes for htmlentities / htmlspecialchars. Do not do this until you know that you are outputting HTML / XML. As soon as you start using escaping / coding / quoting everywhere, you run the risk of using double-coding material and you get useless constructs like "
Marc b
source share