Do PHP PDO prepared statements need to be escaped?

后端 未结 5 661
Happy的楠姐
Happy的楠姐 2020-11-29 06:25

On the PDO::Prepare page it states,

\"and helps to prevent SQL injection attacks by eliminating the need to manually quote the parameters\"

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 07:27

    PDO does not escape the variables. The variables and the SQL command are transferred independently over the MySQL connection. And the SQL tokenizer (parser) never looks at the values. Values are just copied verbatim into the database storage without the possibility of ever causing any harm. That's why there is no need to marshall the data with prepared statements.

    Note that this is mostly a speed advantage. With mysql_real_escape_string() you first marshall your variables in PHP, then send an inefficient SQL command to the server, which has to costly segregate the actual SQL command from the values again. That's why it's often said that the security advantage is only implicit, not the primary reason for using PDO.

    If you concat the SQL command and don't actually use prepared statments (not good!), then yes, there still is an escape function for PDO: $pdo->quote($string)

提交回复
热议问题