11

Possible Duplicate:
SQL injection that gets around mysql_real_escape_string()

I havent seen any valuabe or not outdated info on this. So, there is this question: Does mysql_real_escape_string() FULLY protect against SQL injection? Yet it is very outdated(its from '09), so as of php 5.3 and mysql 5.5 in '12, does it protect fully ?

6
  • 10
    Please for the love of Zaphod Beeblebrox upgrade yourself to prepared statements via PDO or the mysqli extension. mysql* is a dinosaur that should never be used. If so many "tutorial" sites didn't stubbornly continue to use it, the PHP dev team would've already deprecated it. New devotees to this backwards practice are minted daily because they don't know any better and after all, "the tutorial said I could do it this way." Prepared statements are the correct way to prevent SQL injection. Commented Mar 22, 2012 at 0:15
  • 1
    Source: news.php.net/php.internals/53799 Commented Mar 22, 2012 at 0:25
  • thank you @rdlowrey, my head was thinking "everyones using mysql, how bad can it be !?" apparently, you are right. Commented Mar 22, 2012 at 0:57
  • I have researched a bit about mysqli, it seems like that I dont have to use "mysql_real_escape_string" at all. and, sql injection is impossible and everything I insert is actual text without me worring about anything. I just want to confirm this, is it true ? Commented Mar 22, 2012 at 1:11
  • 1
    @w8ph mysqli does have an equivalent method: mysqli_real_escape_string. The thing is, the whole point of my comment is that you should use prepared statements, which negates the possibility of injection without the need for manual escaping. Take a look at the docs for mysqli::prepare for more info. Commented Mar 22, 2012 at 3:28

3 Answers 3

10

mysql_real_escape_string ALONE can prevent nothing.

Moreover, this function has nothing to do with injections at all.

Whenever you need escaping, you need it despite of "security", but just because it is required by SQL syntax. And where you don't need it, escaping won't help you even a bit.

The usage of this function is simple: when you have to use a quoted string in the query, you have to escape it's contents. Not because of some imaginary "malicious users", but merely to escape these quotes that were used to delimit a string. This is extremely simple rule, yet extremely mistaken by PHP folks.

This is just syntax related function, not security related.

Depending on this function in security matters, believing that it will "secure your database against malicious users" WILL lead you to injection.

A conclusion that you can make yourself:
No, this function is not enough.

Prepared statements is not a silver bullet too. It covers your back for only half of possible cases. See the important addition I made to the famous question for the details

Sign up to request clarification or add additional context in comments.

Comments

4

long time since I read a blog post about this so it may no longer hold true BUT...

The posts stated that if you had unicode encoded characters in your string they would be missed by real escape string but would be evaluated by mysql engine - alluding to the idea that you could indeed still be open to a well placed injection.

I can't remember the blog post but this question on here is in the same ball-park.

Comments

0

Yes. By properly escaping the string using the native mysql escape functions, it's not possible to "break out" and execute a query.

However, a better approach would be to use prepared statements. This will do a number of things. By using prepared statements you take advantage of even more optimization from the database and it will properly escape any data passed in. Take a look at: http://php.net/manual/en/mysqli.prepare.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.