使用 PDOStatement 的优点?
使用 PDOStatement 代替常规 Mysql 有什么区别/优点?
what is the difference / advantage of using PDOStatement instead of regular Mysql?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定你所说的“常规 MySQL”是什么意思,但我担心你可能指的是字符串连接查询。因此,使用像 PDO 这样的框架最直接的优势就是使用准备好的参数化查询。这降低了 SQL 注入的风险(但不一定消除它,所以不要这么认为),同时,根据数据库服务器的不同,可能允许服务器更好地缓存查询执行计划以提高性能。
在更高的层面上,使用这样的框架背后的想法是将实际的数据访问组件从应用程序逻辑中抽象出来一点,这始终是一个值得实现的目标。基本上,您不是直接与数据库交互,而是主要与数据访问框架交互(在这种情况下,它仍然可以使您与数据库实现紧密耦合,因此请注意这一点)并让它处理数据库访问。这种方法的主要好处之一是,如果您更改数据库实现(例如从 MySQL 迁移到 PostgreSQL),则无需更改代码。您只需使用后者的 PDO 实现作为前者的直接替代品。
I'm not sure what you mean by "regular MySQL" but I'm concerned that you might mean string-concatenated queries. So the most direct advantage of using a framework like PDO is to use prepared and parameterized queries. This reduces the risk of SQL injection (but doesn't necessarily eliminate it, so don't think that) and at the same time, depending on the database server, may allow the server to better cache query execution plans to improve performance.
At a higher level, the idea behind using a framework like this is to abstract the actual data access componentry out of the application logic a little more, which is always a worthy goal. Basically, rather than interfacing directly with the database, you'd be interfacing primarily with the data access framework (which in this case can still keep you tightly coupled with the database implementation, so be aware of that) and let it handle the database access. One of the primary benefits to this approach is that, if you change a database implementation (such as move from MySQL to PostgreSQL) then you don't need to change your code. You just use the latter's implementation of PDO as a drop-in replacement for the former's.