use it on the actual values in your query, not the whole query string itself.

example:

$username = mysql_real_escape_string($_POST['username']);
$query = "update table set username='$username' ...";
$rs = mysql_query($query);
Answer from CrayonViolent on Stack Overflow
🌐
PHP
php.net › manual › en › function.mysql-real-escape-string.php
PHP: mysql_real_escape_string - Manual
Escapes special characters in the ... library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a....
🌐
MySQL
dev.mysql.com › doc › c-api › 9.7 › en › mysql-real-escape-string.html
MySQL :: MySQL 9.7 C API Developer Guide :: 5.4.60 mysql_real_escape_string()
Characters encoded are \, ', ", NUL (ASCII 0), \n, \r, and Control+Z. Strictly speaking, MySQL requires only that backslash and the quote character used to quote the string in the query be escaped. mysql_real_escape_string() quotes the other characters to make them easier to read in log files.
Discussions

mysql - How to use mysql_real_escape_string function in PHP - Stack Overflow
So in this program I'm writing, I actually grab a SQL query from the user using a form. I then go on to run that query on my database. I know not to "trust" user input, so I want to do sanitizati... More on stackoverflow.com
🌐 stackoverflow.com
bypass "mysql_real_escape_string" - Security - Hak5 Forums
Hello, I where wondering if someone has ever bypass this function mysql_real_escape_string "mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a. " For example I want to inout the... More on forums.hak5.org
🌐 forums.hak5.org
July 27, 2016
PDO - mysql_real_escape_string - PHP - SitePoint Forums | Web Development & Design Community
OK. Just started looking into PDO and converting my old mysql_connects to PDO. One thing I cant seem to find is if PDO has a method similar to mysql_real_escape_string. Does it even need one? More on sitepoint.com
🌐 sitepoint.com
0
December 30, 2007
is mysql_real_escape_string enough?
You should read up on PDO, its perfectly crafted for handling issues such as injections with prepared/binded statements. PDO Intorduction PDO Prepared Statements More on reddit.com
🌐 r/PHP
45
21
April 17, 2011
🌐
Reddit
reddit.com › r/php › did you know that mysql_real_escape_string() and mysqli_escape_string() are **not** sufficient for securing an app?
r/PHP on Reddit: Did you know that mysql_real_escape_string() and mysqli_escape_string() are **not** sufficient for securing an app?
January 3, 2011 - Well, mysql_real_escape_string doesn't protect against sql injections more than addslashes, but that's not the reason you use it. addslashes() was from the developers of PHP whereas mysql_real_escape_string uses the underlying MySQL C++ API (i.e. from the developers of MySQL).
🌐
W3Schools
w3schools.com › php › func_mysqli_real_escape_string.asp
PHP mysqli real_escape_string() Function
The real_escape_string() / mysqli_real_escape_string() function escapes special characters in a string for use in an SQL query, taking into account the current character set of the connection.
🌐
MariaDB
mariadb.com › docs › connectors › mariadb-connector-c › api-functions › mysql_real_escape_string
mysql_real_escape_string | Connectors | MariaDB Documentation
August 10, 2026 - mysql_real_escape_string encodes a string for safe use in a SQL statement, taking the connection's current character set into account when escaping special characters.
Find elsewhere
🌐
Hak5
forums.hak5.org › talk › security
bypass "mysql_real_escape_string" - Security - Hak5 Forums
July 27, 2016 - Hello, I where wondering if someone ... library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a. " For example I want to inout the......
🌐
SitePoint
sitepoint.com › php
PDO - mysql_real_escape_string - PHP - SitePoint Forums | Web Development & Design Community
December 30, 2007 - OK. Just started looking into PDO and converting my old mysql_connects to PDO. One thing I cant seem to find is if PDO has a method similar to mysql_real_escape_string. Does it even need one?
🌐
GeeksforGeeks
geeksforgeeks.org › sql › sql-injection-that-gets-around-mysql-real-escape-string
SQL Injection that Gets Around mysql_real_escape_string() Function - GeeksforGeeks
July 23, 2025 - SQL injection is a serious security vulnerability that occurs when an attacker can manipulate SQL queries executed in a database. In an attempt to prevent SQL injection, developers often use functions like mysql_real_escape_string() in PHP to escape special characters.
🌐
Reddit
reddit.com › r/php › is mysql_real_escape_string enough?
r/PHP on Reddit: is mysql_real_escape_string enough?
April 17, 2011 -

I'm still new to PHP and as part of my self education I'm hand-coding a CMS. So far all I've managed to make it do is dynamically pull page data. What content gets displayed depends on the value given in a $_GET variable. If one isn't present, it defaults to the homepage. That took a while to figure out...so many errors...

But anyway, I realized that since the GET ends up being part of my mysql query, I should probably secure it against SQL injection. My question is if running GET through mysql_real_escape_string() is enough to do that? Is there more I should/could be doing?

Or do I even need it in this specific case? This is how the $_GET is being used in my query-

$pull_page_data = mysql_query("SELECT * FROM pages WHERE page_id = '$page'");

Can havoc still be caused at the end of the query?

But back to the primary question, is doing this enough to secure my database from a basic sql injection?

$page = mysql_real_escape_string($_GET['page']);

Thanks, guys!

Top answer
1 of 6
88

The function adds an escape character, the backslash, \, before certain potentially dangerous characters in a string passed in to the function. The characters escaped are

\x00, \n, \r, \, ', " and \x1a.

This can help prevent SQL injection attacks which are often performed by using the ' character to append malicious code to an SQL query.

2 of 6
61

Say you want to save the string I'm a "foobar" in the database.
Your query will look something like INSERT INTO foos (text) VALUES ("$text").
With the $text variable replaced, this will look like this:

INSERT INTO foos (text) VALUES ("I'm a "foobar"")

Now, where exactly does the string end? You may know, an SQL parser doesn't. Not only will this simply break this query, it can also be abused to inject SQL commands you didn't intend.

mysql_real_escape_string makes sure such ambiguities do not occur by escaping characters which have special meaning to an SQL parser:

mysql_real_escape_string($text)  =>  I\'m a \"foobar\"

This becomes:

INSERT INTO foos (text) VALUES ("I\'m a \"foobar\"")

This makes the statement unambiguous and safe. The \ signals that the following character is not to be taken by its special meaning as string terminator. There are a few such characters that mysql_real_escape_string takes care of.

Escaping is a pretty universal thing in programming languages BTW, all along the same lines. If you want to type the above sentence literally in PHP, you need to escape it as well for the same reasons:

$text = 'I\'m a "foobar"';
// or
$text = "I'm a \"foobar\"";
🌐
MySQL
dev.mysql.com › doc › c-api › 8.0 › en › mysql-real-escape-string.html
MySQL :: MySQL 8.0 C API Developer Guide :: 5.4.60 mysql_real_escape_string()
Characters encoded are \, ', ", NUL (ASCII 0), \n, \r, and Control+Z. Strictly speaking, MySQL requires only that backslash and the quote character used to quote the string in the query be escaped. mysql_real_escape_string() quotes the other characters to make them easier to read in log files.
🌐
PHPBuilder
board.phpbuilder.com › d › 10335128-resolved-mysql-real-escape-string-breaks-n-need-a-fix
[RESOLVED] Mysql_real_escape_string breaks \n, need a fix - PHPBuilder Forums
January 23, 2007 - The title says it all... I have just added mysql_real_escape_string to my _POST parsing function, and it breaks \n, making nl2br() completely unable to read...
Top answer
1 of 3
2

You should never use anything "to reduce the likelihood of SQL injections" in the first place. It just doesn't work this way. You are not adding some code just in case, sort of a rabbit foot. SQL injections simply must be prevented, using certain tools. It is so easy to achieve that it just should go without saying. Just never add any variable to your SQL queries - that's all. Here is how. It doesn't only prevent injections but also relieves you from that escaping mess.

That said, no extra slashes should appear in the output unless there is an error in your code. Escaping is used for the query only, it is not stored in the database. So instead of removing extra slashes you must not add them in the first place.

2 of 3
2

No, it's not safe, because what you are doing is wrong. In your code you are echoing a variable that has only been sanitized using mysqli_real_escape_string (https://www.php.net/manual/en/function.mysql-real-escape-string.php). All that function does is prepend backslashes to some characters, like the single quote, the new line character, etc. This function was only meant to sanitize a string before using it in an SQL statement. By the way, today it is considered bad practice to use such a function, and the recommended way to make queries to the database is to use prepared statements. It's a totally different way to make queries, and you will need to use another set of PHP functions and objects.

To echo something on an HTML web page you can't use mysqli_real_escape_string for sanitization, because the set of dangerous characters is different. In HTML, you need to escape the < and > characters, for example. Other characters might have to be escaped depending on where you are echoing them, for example the single or double quote. Also, the escaping method is different from the one used for SQL: a single quote in HTML won't be prepended with a backslash when escaped, but will need to be replaced with an HTML entity, becoming &apos;. A PHP function you might use for this is htmlspecialchars (https://www.php.net/manual/en/function.htmlspecialchars.php).

🌐
SitePoint
sitepoint.com › php
Using mysql_real_escape_string and sprintf - PHP - SitePoint Forums | Web Development & Design Community
January 11, 2011 - I understand what mysql_real_escape_string does and why it is necessary to clean inputs before passing them to an sql query. What I don’t understand is why the php manual shows the sql query being cleaned by mysql_real_escape_string all wrapped inside sprintf.
🌐
SitePoint
sitepoint.com › php
Magic quotes and MySQL real escape string - PHP - SitePoint Forums | Web Development & Design Community
January 16, 2010 - I’ve used PHP for years but have only recently become aware of what magic quotes is and does and also that once PHP 6 arrives to our hosters we will no longer be able to use it. Can I please check that I understand it correctly? With magic quotes on you don’t need to escape anything as it’s done automatically on all GET, POST and COOKIE data.
🌐
Facebook
facebook.com › groups › phpmasters › posts › 10157100367337836
Is mysqli real escape string secure or should I use prepare ...
Popular groups · Find communities for you · Over 1 billion people across the globe are using Facebook Groups to explore their favorite topics · Log in · Categories · Science & tech · Travel · Animals · Sports & fitness · Entertainment
🌐
Baeldung
baeldung.com › home › security › sql injection that gets around mysql_real_escape_string()
SQL injection that gets around mysql_real_escape_string() Baeldung on SQL
August 12, 2025 - When the application sets NAMES gbk, the server starts interpreting incoming strings as GBK-encoded data, while the client library in PHP might still believe it is using latin1. Consequently, mysql_real_escape_string() inserts a backslash in front of 0x27, thinking that it is neutralizing a single quote in latin1.