tags:

views:

646

answers:

2

Hello,

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 INSERTs. The site is currently not live and so making changes now is easy.

In you 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?

Thank you

+3  A: 

PDO has the following advantages (that I know of):

  • It's cross database, meaning it's the same interface for different relation databases.
  • It's faster.
  • It helps protect against SQL injections.
  • It's cleaner IMO.

Also, this question has been asked before, you may want to take a look at the answers:

musicfreak
Thanks for the quick response. Yes I saw those other posts which were helpful. I was more wondering if it would be worth while to switch over to PDO. I wasn't going to switch over but now that I have learned about PDO I was curious if I should switch over, or if staying with MYSQL would be fine.
justinl
Well if you already have a lot of code written, I wouldn't bother, but for any new projects I would strictly suggest PDO.
musicfreak
Yes there's a fair amount of code. I wish I had learned about it earlier. But it's good to hear that it's not the end of the world if I don't use it. Thanks again!
justinl
I agree it's cleaner. I don't know if being cross-database is really an advantage. It only helps protect against SQL injection if you use it right. It is NOT necessarily faster, I don't believe you can make such a statement credibly without citing a benchmark.
MarkR
@MarkR: I've seen many benchmarks prove that it is faster. Google it. And how is cross-database not an advantage?
musicfreak
+1  A: 

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

Coded Signal