View stunning SlideShares in full-screen with the new iOS app!Introducing SlideShare for AndroidExplore all your favorite topics in the SlideShare appGet the SlideShare app to Save for Later — even offline
View stunning SlideShares in full-screen with the new Android app!View stunning SlideShares in full-screen with the new iOS app!
PHP Data Objects, (PDO) is a PHP5 extension that defines a lightweight DBMS connection abstraction library (sometimes called data access abstraction library).
PDO provides a data-access abstraction layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data.
This means that PDO defines a unified interface for creating and maintaining database connections, issuing queries, quoting parameters, traversing result sets, dealing with prepared statements, and error handling.
PDO ships with PHP 5.1, and is available as a PECL extension for PHP 5.0; PDO requires the new OO features in the core of PHP 5, and so will not run with earlier versions of PHP.
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.
With PDO, we will always have to explicitly use the connection object, since there is no other way of calling its methods.
Each of the three above connections could be established in the following manner:
$conn = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
$conn = new PDO("sqlite:$db");
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 first argument passed to the PDO constructor. For SQLite, which does not utilize username and password, the second and third arguments can be skipped.
SQLite is not a database server, but it is an embedded SQL database library that operates on local files. More information about SQLite can be found at www.sqlite.org and more information about using SQLite with PHP can be found at www.php.net/sqlite.
Connecting to the Database – 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.
Their advantage over using traditional methods of creating database connection is that you don't have to modify your code if you change the database management system.
A connection string can be defined in a configuration file and that file gets processed by your application. Should your database (data source) change, you just edit that configuration file and the rest of your code is kept intact.
Connecting to the Database – Connection Strings
Create the connection object
$conn = new PDO( $connStr , $user, $pass);
DB connection string and username/password
$connStr = 'mysql:host=localhost;dbname=pdo';
$user = 'root';
$pass = 'root';
The three connection strings looked like the following:
As we can see, the prefix (the substring before the first semicolon) always keeps the name of the PDO driver.
Since we don't have to use different functions to create a connection with PDO, this prefix tells us which internal driver should be used. The rest of the string is parsed by that driver to further initiate the connection.
Now that we are using PDO, we can do the following:
Assume the $connStr variable holds a valid connection string
$sql = 'SELECT DISTINCT make FROM cars ORDER BY make';
$conn = new PDO($connStr, 'boss', 'password');
$q = $conn->query($sql);
As you can see, doing things the PDO way is not too different from traditional methods of issuing queries. Also, here it should be underlined, that a call to $conn->query() is returning another object of class PDOStatement , unlike the calls to mysql_query(), sqlite_query(), and pg_query(), which return PHP variables of the resource type.
The PDO class defines a single method for quoting strings so that they can be used safely in queries.
$m = $conn->quote($make);
$q = $conn->query("SELECT sum(price) FROM cars WHERE make=$m");
Now that we have issued our query, we will want to see its results. As the query in the last example will always return just one row, we will want more rows. Again, the three databases will require us to call different functions on the $q variable that was returned from one of the three calls to mysql_query(), sqlite_query(), or pg_query(). So our code for getting all the cars will look similar to this:
As you may already have guessed, things are pretty straightforward when it comes to PDO: We don't care what the underlying database is, and the methods for fetching rows are the same across all databases. So, the above code could be rewritten for PDO in the following way:
mysql_fetch_array(), sqlite_fetch_array() without the second parameter, or pg_fetch_array().)
$q = $conn->query("SELECT DISTINCT make FROM cars ORDER BY
while($r = $q->fetch( PDO::FETCH_ASSOC ))
echo $r['make'], "
Nothing is different from what happens before. One thing to note here is that we explicitly specified the PDO::FETCH_ASSOC fetch style constant here, since PDO's default behavior is to fetch the result rows as arrays indexed both by column name and number. (This behavior is similar to mysql_fetch_array(), sqlite_fetch_array() without the second parameter, or pg_fetch_array().)