我们如何知道 PDO 正在逃避 SQL 注入?
我是 PDO 库的新手。我正在使用 mysql 作为数据库的开发环境。我可以在使用“?”时使用准备和执行函数来运行查询使用命名占位符(例如:“:column”)时的占位符和bindParam方法。
之后,我尝试看看 PDO 是否通过放入任何引号来清理查询,就像 mysql_real_escape_string 那样进行任何类型的转义。我试图查看查询会是什么样子,但我得到的只是已传递到准备语句中的语句,而不是要执行的查询。
我尝试 var_dump $result->execute() 和 $result->fetch() 但execute 语句为我提供了带有占位符的准备语句的sql,而fetch 语句为我提供了该查询的结果。
有没有办法查看将运行的查找查询,或者至少在运行查询之前查看参数的外观?
我希望我的问题很清楚。 :|
I am newbie with PDO libraries. I am working on development environment with mysql as my database. I am able to run through my queries using prepare and execute function while using "?" placeholder and also bindParam method while using named placeholders (ex: ":column").
After this I tried to see if PDO does any kind of escaping by putting in any quotes to sanitize the query like mysql_real_escape_string does. I am trying to see what would the query look but all I get is the statement that has been passed into the prepare statement, but not the query that would be executed.
I tried to var_dump the $result->execute(), and $result->fetch() but the execute statement gives me my prepare statement's sql with place holders while fetch statement gives me the result of that query.
Is there a way to look at the find query that would be run, or atleast how the parameters would look before running the query??
I hope I am clear with my question. :|
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您编写如下内容时:
实际查询是...
SELECT * FROM tbl_name WHERE col_name = :col_name;
。这就是所谓的“准备好的声明”。首先,将查询发送到数据库,然后发送查询参数。 PDO 不合并查询和参数。您可能认为
PDOStatement::bindValue()
会执行以下操作:但它不会。
它的作用更像是:
阅读有关准备好的语句的更多信息
When you write something like:
The actual query is...
SELECT * FROM tbl_name WHERE col_name = :col_name;
. That's called prepared statement. Firstly, you send query to the database, later you send query parameters. PDO doesn't merge query and parameters.You've probably thought that
PDOStatement::bindValue()
does something like:But it doesn't.
It does something more like that:
Read more about Prepared Statements
说实话。
PDO 有 2 种运行准备语句的模式:
?
标记(但没有命名占位符,这些占位符被 PDO 替换为?
)这两种方法都是完全安全的。
当你有一个变量标识符时,真正的危险就开始了......
To put it straight.
PDO has 2 modes of running prepared statements:
?
marks (but no named placeholders which being replaced by PDO with?
s)Both methods are perfectly safe.
The real danger begins when you have a variable identifier...
准备语句由mysql处理,因此pdo不会转义请求,
pdo 发送请求和“之后”参数
the prepare statement is handle by mysql, so pdo don't escape the request,
pdo send the request and "after" the parameter
启用常规查询日志,并在运行简单语句时观察实际在服务器上执行的查询 - 执行一些插入,例如使用包含嵌入引号或 null 的字符串。
Enable the general query log, and watch the queries actually being executed against the server when you're running simple statements - do some inserts, for example, with strings containing embedded quotes or nuls.