Re-introducing PDO – the Right Way to Access Databases in PHP
PDO is the acronym of PHP Data Objects. As the name implies, this extension gives you the ability to interact with your database through objects.
The very valid question people ask when confronted by a new technology is simply, why should they upgrade? What does this new technology give them that is worth the effort of going through their entire application and converting everything to this new library, extension, or whatever?
It’s a very valid concern. We’ve written about this to some degree before, but let’s go through why we think it’s worth it to upgrade.
PDO is object-oriented
Let’s face it: PHP is rapidly growing, and it is moving toward becoming a better programming language. Usually, when this happens in a dynamic language, the language increases its strictness in order to allow programmers to write enterprise applications with peace of mind.
In case of PHP, better PHP means object-oriented PHP. This means the more you get to use objects, the better you can test your code, write reusable components, and, usually, increase your salary.
Using PDO is the first step in making the database layer of your application object-oriented and reusable. As you will see in the rest of this article, writing object-oriented code with PDO is much simpler than you may think.
Imagine that you have written a killer application using MySQL at your current workplace. All of a sudden, someone up the chain decides that you must migrate your application to use Postgres. What are you going to do?
You have to do a lot of messy replaces, like converting mysql_connect or mysqli_connect to pg_connect, not to mention all the other functions you used for running queries and fetching results. If you were using PDO, it would be very simple. Just a few parameters in the main configuration file would need changing, and you’d be done.
It allows parameter binding
Parameter binding is a feature that allows you to replace placeholders in your query with the value of a variable. It means:
- You don’t have to know, at runtime, how many placeholders you will have.
- Your application will be much safer against SQL injection.
You can fetch data into objects
People who have used ORMs like Doctrine know the value of being able to represent data in your tables with objects. If you would like to have this feature, but don’t want to learn an ORM, or don’t want to integrate it into an already existing application, PDO will allow you to fetch the data in your table into an object.
mysql extension is no longer supported!
mysql extension is finally removed from PHP 7. That means if you’re going to use PHP 7, you need to change all those functions to
mysqli_* instead of
mysql_*. This is a great time to just upgrade straight to PDO because of how much it helps you in writing maintainable, portable code with much less effort.
I hope the reasons above have convinced you to start integrating PDO into your application. Don’t worry about setting it up; you may already have it on your system!
Continue reading %Re-introducing PDO – the Right Way to Access Databases in PHP%
LEAVE A REPLY
You must be logged in to post a comment.