Suppose you want to store the string I'm a "foobar" in the database.
Your query will look something like INSERT INTO foos (text) VALUES ("$text") .
When replacing the variable $text it will look like this:
INSERT INTO foos (text) VALUES ("I'm a "foobar"")
Now, where exactly does the line end? You may know that the SQL parser does not. Not only will this break this query, it can also be used to enter SQL commands that you did not plan.
mysql_real_escape_string ensures that such ambiguities do not occur, escaping characters of particular importance to the SQL parser:
mysql_real_escape_string($text) => I\'ma \"foobar\"
This will:
INSERT INTO foos (text) VALUES ("I\'ma \"foobar\"")
This makes the statement unambiguous and safe. The signal \ signals that the next character should not be perceived by its special value as a line terminator. There are several such characters that mysql_real_escape_string cares about.
Escaping is a pretty universal thing in BTW programming languages, all in the same direction. If you want to print the above sentence literally in PHP, you also need to avoid it for the same reasons:
$text = 'I\'ma "foobar"'; // or $text = "I'm a \"foobar\"";
deceze Jun 13 2018-11-11T00: 00Z
source share