MySQL 与 PDO
我对 PHP 相当陌生,并且已经使用标准 MySQL 数据库调用构建了一个中型网站。 然而,我最近了解了 PDO,我希望从社区中了解是否值得从 MySQL 切换到 PDO。 为了安全起见,我一直在使用mysql_real_escape_string
。
有关该网站的信息:
我混合使用了 INSERT 和 SELECT 调用。 SELECT
调用返回的数据并不大(使用 LIMIT
返回的记录不超过 30 条)。 也不会有大量的INSERT
。 该网站目前尚未上线,因此现在进行更改很容易。
以您的专业意见,我是否值得花时间将站点从 MySQL 切换到 PDO? 或者继续使用 MySQL 也同样好吗? 或者换句话说,如果有的话,现在切换到 PDO 的原因是什么?
I'm fairly new to PHP and have built a medium sized website using standard MySQL database calls. However, I have recently learned about PDO and I am hoping to find out from the community if it is worth switching from MySQL over to PDO. For security I have been using mysql_real_escape_string
.
Info about the site:
I'm using a mix of INSERT
and SELECT
calls. The data returned from SELECT
calls isn't massive (no more than 30 records returned by using LIMIT
). There will also not be a whole lot of INSERT
s. The site is currently not live and so making changes now is easy.
In your professional opinions, is it worth my time to switch the site over to PDO from MySQL? Or is staying with MySQL just as good? Or in other words, what would be the reason, if any, to switch to PDO now?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与 mysql_* 函数相比,PDO 具有以下优点:
这个问题之前已经被问过,您可能想看看答案:
如果您要开始一个新项目,我强烈建议使用 PDO 或更高级别的库/ORM。 如果您已经在没有它的情况下编写了很多代码,那么可能不值得。
PDO has the following advantages over the mysql_* functions:
This question has been asked before, you may want to take a look at the answers:
If you are starting a new project, I would strictly suggest using PDO or a higher-level library/ORM. If you already have a lot of code written without it, it may not be worth it.
PDO 具有上面链接的页面中列出的优点:数据库抽象(即您的代码可以跨各种类型的数据库移植),如果您使用其准备好的语句功能,它可以为您处理许多常见的安全问题,并且它以 Class 实例的形式返回结果(它本身可以通过鼓励面向对象的方法极大地改进您的代码)等等。
但是,最好的方法是您查看 ORM 库,例如 Doctrine。 考虑到项目的规模,这似乎有些过大,但坦率地说,开始学习最佳实践永远不会太早。 Zend Framework 的首席开发人员提供了关于如何构建防弹、可维护的数据库驱动应用程序的精彩概述,可以观看:
http://mtadata.s3.amazonaws.com/webcasts/20090724-playdoh。 .wmv
PDO has the advantages listed over at the pages linked to above: database abstraction (i.e. your code is made portable across a variety of flavours of DB), it handles a lot of the usual security issues for you if you use its prepared statement features and it returns results as Class instances (which by itself can greatly improve your code by encouraging an object oriented approach), etc., etc.
However the very best approach would be for you to look into an ORM library such as Doctrine. It may seem like overkill given the size of your project, but frankly it's never too early to start learning best practice. An excellent overview of how to build bullet-proof, maintainable database-driven apps given by the lead developer of the Zend Framework can be watched at
http://mtadata.s3.amazonaws.com/webcasts/20090724-playdoh.wmv