Introduction PHP Data Objects

 PHP Data Objects, (PDO) is a PHP5 extension that defi  nes a lightweight DBMS
connection abstraction library (sometimes called data access abstraction library).
The need for a tool like PDO was dictated by the great number of database systems
supported by PHP. Each of these database systems required a separate extension
that defi  ned its own API for performing the same tasks, starting from establishing a
connection to advanced features such as preparing statements and error handling.
The fact that these APIs were not unifi  ed made transition between underlying
databases painful, often resulting in the rewriting of many lines of code, which in
turn, led to new programming errors that required time to track, debug and correct.
On the other hand, the absence of a unifi  ed library, like JDBC for Java, was putting
PHP behind the big players in the programming languages world. Now that such
library exists, PHP is regaining its position and is a platform of choice for millions
of programmers.
It should be noted, however, that there exist several libraries written in PHP, that
serve the same purpose as PDO. The most popular are the ADOdb library and the
PEAR DB package. The key difference between them and PDO is speed. PDO is a
PHP extension written in a compiled language (C/C++), while the PHP libraries
are written in an interpreted language. Also, once PDO is enabled, it does not
require you to include source fi  les in your scripts and redistribute them with your
application. This makes installing your applications easier, as the end user does not
need to take care of third-party software.
PDO being a PECL extension, itself relies on database-specifi  c drivers and on other
PECL extensions. These drivers must also be installed in order to use PDO (you only
need the drivers for the databases you are using). Since the description of installation
of PDO and database-specifi  c drivers is beyond the scope of this book, you can refer
to PHP manual at www.php.net/pdo for technical information regarding installation
and upgrade issues.
Using PDO
As it has been noted in the previous section, PDO is a connection, or data access
abstraction library. This means that PDO defi  nes a unifi  ed interface for creating and
maintaining database connections, issuing queries, quoting parameters, traversing
result sets, dealing with prepared statements, and error handling.
We will give a quick overview of these topics here and look at them in greater detail
in the following chapters.
Connecting to the Database
 Let's consider the well-known MySQL connection scenario:
mysql_connect($host, $user, $password);
mysql_select_db($db);
Here, we establish a connection and then select the default database for the
connection. (We ignore the issue of possible errors.)
In SQLite, for example, we would write something like the following:
$dbh = sqlite_open($db, 0666);
Here again we ignore errors (we will cover more on this later). For completeness,
let's see how we would connect to a PostgreSQL:
pg_connect("host=$host dbname=$db user=$user password=$password");
As you can see, all three databases require quite different ways of opening a
connection. While this is not a problem now, but if you always use the same database
management system in case you need to migrate, you will have to rewrite
your scripts.
Now, let's see what PDO has to offer. As PDO is fully object-oriented, we will be
dealing with connection objects, and further interaction with the database will
involve calling various methods of these objects. The examples above implied the
need for something analogous to these connection objects—calls to mysql_connect
or pg_connect return link identifi  ers and PHP variables of a special type: resource.
However, we didn't use connection objects then since these two database APIs
don't require us to explicitly use them if we only have one connection in our scripts.
However, SQLite always requires a link identifi  er.
With PDO, we will always have to explicitly use the connection object, since there
is no other way of calling its methods. (Those unfamiliar with object-oriented
programming should refer to Appendix A).
Each of the three above connections could be established in the following manner:
// For MySQL:
$conn = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
// For SQLite:
$conn = new PDO("sqlite:$db");
// And for PostgreSQL:
$conn = new PDO("pgsql:host=$host dbname=$db", $user, $pass);
As you can see, the only part that is changing here is the fi  rst argument passed to the
PDO constructor. For SQLite, which does not utilize username and password, the
second and third arguments can be skipped.
Connection Strings
As you have seen in previous example, PDO uses the so-called connection strings
(or Data Source Names, abbreviated to DSN) that allow the PDO constructor to select
proper driver and pass subsequent method calls to it. These connection strings or
DSNs are different for every database management system and are the only things
that you will have to change.
If you are designing a big application that will be able to work with different
databases, then this connection string (together with a connection username and
a password) can be defi  ned in a confi  guration fi  le and later used in the following
manner (assuming your confi  guration fi  le is similar to php.ini)
$config = parse_ini_file($pathToConfigFile);
$conn = new PDO($config['db.conn'], $config['db.user'],
                $config['db.pass']);
Your confi  guration fi  le might then look like this:
db.conn="mysql:host=localhost;dbname=test"
db.user="johns"
db.pass="mypassphrase"
We will cover connection strings in more detail in Chapter 2; here we gave a quick
example so that you can see how easy it is to connect to different database systems
with PDO.