mysql_real_escape_string()
can fail to clean the input.
Since mysql_real_esacpe_string()
takes character set into account while cleaning strings.
There's the problem. You can change character via mysql_query
function sending the query to change connection's character set. However, mysql_real_escape_string()
is oblivious to the set you're using and it will escape some characters improperly.
The other thing is constantly invoking it manually. Even wrapping it in a function is a P.I.T.A. because it implies you have to create some sort of database wrapper / database abstraction layer of your own in order to be able to automate calls to mysql_real_escape_string()
.
That's why we have PDO in PHP which helps us alleviate all of the above and you get to use parametrized prepared statements which are preferable way of dealing with repeating queries that alter the data.
Prepare the statement, bind input variables and it will clean the input according to the database driver being used and connection's character set. It's less code and completely safe.