Is pg_escape_string or mysql_escape_string enough to sanitize a string before inserting data into a database table?
Is pg_escape_string or mysql_escape_string enough to sanitize a string before inserting data into a
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The word “sanitize” is highly questionable. It implies a worldview where certain characters are “bad” and have to be filtered at source. This is misguided.
Getting text in suitable format to go in an SQL query is about escaping out-of-band characters to their SQL literal form, not about removing “bad” characters. If you want to validate user input on entry to your application (eg. verifying a telephone number has no letters in it, or getting rid of unwanted control characters) then that’s fine. But that’s an application-specific validation concern, and an entirely different issue to anything to do with SQL-escaping or HTML-escaping. Those are output-stage concerns.
mysql_escape_stringis potentially not enough to safely escape text for inclusion in an SQL string literal. On a connection that might be using some East Asian character sets as the encoding, or some non-default SQL syntax options, it will generate malformed strings that can permit SQL-injection.mysql_real_escape_stringis better. However, parameterised queries avoid the issue and are to be preferred where available.pg_escape_stringuses the connection, likemysql_real_escape_stringdoes, so I would expect it to be safe. But still, parameters! Inpg_you getpg_query_paramsso there’s no excuse not to use them.