0

If I properly prepare and bind every query in my application, is there any need for using mysqli_real_escape_string (for MYSQLI connections) or addslashes (for PDO connections)?

I ask this seemingly "already asked" question because elsewhere on Stackoverflow people have asked whether their application is safe from injection attack if they prepare their statements BUT THEY DON'T ALSO SAY BIND.

Typically what follows is a long and helpful explanation from a reader on how first-order queries are safe when the queries are prepared, but second-order queries are vulnerable to injection attack, and then the reader/responder lists things to do to prevent this. And usually BINDING comes up as one solution.

So, my question is actually rather simple:

If every single query in my application is PREPARED with all parameters BOUND using the proper methodology (for MYSQLI or PDO), am I free to drop mysqli_real_escape_string() and addslashes() sanitizing on the strings passed to the queries?

(I'm revising code written by someone else who didn't use a single prepared query in the entire site; instead, he relied on mysqli_real_escape_string for sanitization. Since I'm going to the trouble of preparing and binding every single query, I'd like to be able to drop the mysqli_real_escape_string/addslashes routines if they are made unnecessary by properly preparing all the queries AND binding all the parameters, as I am doing).

Many thanks!

Tom
  • 1,836
  • 4
  • 16
  • 30
  • 1
    *"If I properly prepare and bind every query in my application, is there any need for using mysqli_real_escape_string"* - No. Read the manuals. http://www.php.net/manual/en/mysqli.prepare.php - http://www.php.net/manual/en/pdo.prepare.php – Funk Forty Niner Aug 23 '15 at 15:12
  • 2
    Your question is already spot on. You do in fact need to `->prepare` the query *and* `->bind` all used values. That'll fix most SQL code/data syntax issues, and frees you from manually escaping. SQL injection safety is just a by-product of more robust/readable code. (Binding vars does more or less escape or cast the variables implicitly). - Take note that only values can be bound however: table/column names still need whitelisting. – mario Aug 23 '15 at 15:18
  • 1
    X-Ref: [How can I prevent SQL-injection in PHP?](http://stackoverflow.com/q/60174) (as pointed out by @Fred-ii-) – mario Aug 23 '15 at 15:24

0 Answers0