Welcome to the Treehouse Community
Looking to learn something new?
Mayur PandeCourses Plus Student 11,711 Points
Escaping special characters
Hi I am a little confused about escaping special characters. Hampton says that it is useful to escape special characters as we never want to trust user data. Why is this?
One such reason to escape characters is because they can be used to do malicious things. An important example is when you are working with MySQL connections, using mysql_real_escape_string() to escape any characters which could be used to inject data into a database http://php.net/manual/en/function.mysql-real-escape-string.php.
<?php // Connect $link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') OR die(mysql_error()); // Query $query = sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'", mysql_real_escape_string($user), mysql_real_escape_string($password)); ?>
In the above example, if you did not escape user name or password someone could modify the query to select any user, or even drop the database table.