Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
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.