-2

Do i need to escape characters when inserting into prepared statements? Do prepared statements escape the code for me?

alaboudi
  • 3,187
  • 4
  • 29
  • 47

1 Answers1

1

Easier to ask SO community rather than to use Google...

PHP Manual:

Escaping and SQL injection

Bound variables are sent to the server separately from the query and thus cannot interfere with it. The server uses these values directly at the point of execution, after the statement template is parsed. Bound parameters do not need to be escaped as they are never substituted into the query string directly. A hint must be provided to the server for the type of bound variable, to create an appropriate conversion. See the mysqli_stmt_bind_param() function for more information.

Such a separation sometimes considered as the only security feature to prevent SQL injection, but the same degree of security can be achieved with non-prepared statements, if all the values are formatted correctly. It should be noted that correct formatting is not the same as escaping and involves more logic than simple escaping. Thus, prepared statements are simply a more convenient and less error-prone approach to this element of database security.

... assuming you got for mysqli of course

Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66