Do prepared statements in PDO really increase security?

纵然是瞬间 提交于 2019-12-24 04:46:06

问题


I wonder if those prepared statements of PDO really increase security, or if they are just a "cheap" text-replace in the query. The point of prepared statements actually is, that whatever gets inserted as parameter, will not be parsed by the DBMS as part of the instructions itself, so a parameter like

"'; DROP TABLE foobar;"

has no effect and does not break the query. Does anyone know this in detail? I thought to use PDO with prepared statements for preventing sql injection. It turns out that they are hard to use (and don't even work, at least on my local machine), so I want to find this out before wasting much more time with PDO ;-)


回答1:


Creating a prepared statement sends the query-with-wildcards to the server for parsing, and returns a token to call that statement.

A call merely involves sending the data bound to every parameter. This means there will be no parsing of the data (because it's not part of a query string), and that the structure of the query is fixed when the prepared statement is parsed and cannot be altered by injection.

So, yes, a prepared statement definitely increases safety.

It also means you do not have to incur the parsing overhead if you reuse a prepared statement for several requests.




回答2:


Yes, they increase security. PDO or MySQLi also increases speed versus the regular MYSQL methods in PHP because the data is passed in a more compact form.



来源:https://stackoverflow.com/questions/1946535/do-prepared-statements-in-pdo-really-increase-security

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!