自动转义查询中的数据
有没有办法配置 mysql 或 php,以便在查询中自动转义数据值?我读过有关 PDO 的内容,例如,Zend Framework 有一些自动执行此操作的数据库适配器 - 但在服务器端,是否没有任何配置可以避免必须在代码?
谢谢,
大卫
Is there a way to configure mysql or php so that in queries, data values are escaped automatically? I've read about PDO and that Zend Framework, for example, has some Database-Adapters that do it automatically - but on the server side, isn't there any configuration that can be done to avoid having to take care of it in the code?
Thanks,
David
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不完全是,不。这已经被尝试过 - 结果是现在每个人都讨厌的可怕混乱。
没有“神奇功能”可以使查询安全 - 传递给查询的每个参数都需要进行不同的清理(例如字符串与整数)。有些东西(比如动态表和列名)根本无法清理(甚至不能使用数据库库的字符串转义方法),因此您需要将它们与现有表和列的列表进行比较。
使用 PDO 的预准备语句(或 Zend Framework 数据库函数,据我所知,其中包含 PDO)是您可以尽可能少做的最接近的方法。
Not really, no. This has been tried - the result was a terrible mess that everyone hates now.
There is no "magic function" to make a query safe - every argument you pass to the query needs to be sanitized differently (like strings vs. integers). Some things (like dynamic table and column names) can't be sanitized at all (not even using the database library's string escaping method), so you need to compare them against a list of the existing tables and columns.
Using PDO's prepared statements (or the Zend Framework database functions, which AFAIK wrap PDO among others) is the closest you can get to doing as little as possible.