Why should we escape double quotes,single quotes creating queries in PHP? are there any particular benefits when doing that? or it is just a good practice?
Why should we escape double quotes,single quotes creating queries in PHP
1.6k views Asked by user962206 At
3
There are 3 answers
2
On
If you do not escape quotes, The query ends at the place of single quotes. So your query will not be executed successfully!
E.g.
$qry = "SELECT * FROM user WHERE email='[email protected]'";
It works fine but if any one enters email='test'@test.com' then query ends at 'test' only and not find any rows with that one.
So it prevents also a sql injection!
0
On
s, to prevent from SQL injection attacks. To know SQL injection http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php http://www.homeandlearn.co.uk/php/php13p5.html
To prevent PHP Sql injection https://stackoverflow.com/a/60496/781181
It is required to make your queries work and secure. Consider the following code:
The result SQL would become like this:
Which simply doesn't work. It needs to be properly escaped:
The same applies for other special chars.
Prevent SQL injection
Consider this query:
Where
$username
is obtained from$_POST
. If an attacker managed to post string like' OR 1; --
as the$username
then the query becoming this:which is valid and the
WHERE
always evaluates to true and you will have to give good explanation to your angry users.See also: Best way to prevent SQL Injection in PHP