Introduction to PHP 5 PDO

Hi! I’m David Mytton the new SitePoint PHP Blogger and I am going to be writing for this blog for at least the next 3 months. I am going to try and focus on technical posts to provide some useful code and information about new (and old) features in PHP, so hopefully it will be a good learning experience for myself and readers.

A little bit about me:

I have written several articles for SP in the past, mostly reviews or interviews and I have also had a few articles published in International PHP Magazine. I am also the author of Invision Power Board 2: User Guide, a new book for IPB due out very shortly. In addition to writing for print, I also write commercial and open source PHP software through my limited company, Olate, in the UK.

Aside from computing, I do scuba diving and fencing (yes, with swords!).

If you have any ideas for posts for the blog, please get in touch. There is a box on the bottom left of this post “Suggest a Post” which allows you to do this.

Anyway, onto the main post…

[u]Introduction to PHP 5 PDO[/u]

The new PHP Data Objects (PDO) have been covered in a past blog post but I thought that I’d cover it in a little more detail.

After reading the blog post and also Wez Furlong’s blog I had a play around with some PDO code. Although there are already some PHP database abstraction classes available, the advantage of PDO is that it will be available in any PHP installation (so long as it is enabled) at runtime, without any need for recompiling. In addition, since it has been written from scratch specifically for PHP 5, it has a high level of performance. And if you are like me and often only want the key features, then PDO provides all the essential functions you need to connect to different databases. If you need the “full works”, then the PEAR MDB2 package can handle that.

[u]Note:[/u] PHP 5.1 Beta 3 was released on 14th July and included a number of updates to PDO. You can find out about these changes at http://netevil.org/node.php?nid=325

Before we go on, here is an important note from Wez Furlong (the developer of PDO):

Please note that PDO and its drivers are currently in an “alpha” state; this means that we are reasonably sure that there are no major bugs, but that the package is not feature complete

Win an Annual Membership to Learnable,

SitePoint's Learning Platform

  • http://www.sitepoint.com AlexW

    Great opening post, David. Welcome to the gang ;)

  • http://aplosmedia.com/ Eric.Coleman

    Although there are already some PHP database abstraction classes available, the advantage of PDO is that it will be available in any PHP installation (so long as it is enabled) at runtime, without any need for recompiling.

    PDO is -not- a database abstraction layer. The need for something like ADOdb or PEAR::DB is still present to provide an easier method of switching from one database to another and writing portable sql. PDO does -not- provide that level of functionality to programmers. PDO is merely a common interface to all the databases.

    ADOdb has also included a PDO based driver since the very begging of PDO’s existance, infact that was a recent post on John Lim’s blog about PDO. http://phplens.com/phpeverywhere/?q=node/view/209

  • http://www.deanclatworthy.com Dean C

    Eric, David said nowhere in that quote that PDO was a DB-abstraction layer.

  • momos

    Is it possible with PD0 to write ftp-like dns strings, because the ones used here do have something in common with these, but are still slightly different? Let me give a little example; It would be nice to be able to write something like:

    $dns=’mysql://user:password@127.0.0.1/myDatabase';
    try
    {
    $dbo = new PDO($dsn);
    }
    catch(PDOException $e)
    {
    echo $e;
    }

    I ask this so I can start extending my dns-string parser-functions again for yet an other format. They should start standardising these

  • http://www.olate.co.uk Olate

    There is no harm in trying, but I don’t think you can do that momos because the DSN string is database driver specific.

    php.net: In general, a DSN consists of the PDO driver name, followed by a colon, followed by the PDO driver-specific connection syntax

    Have a look at http://www.php.net/manual/en/function.pdo-construct.php – that gives examples of the DSNs for each database driver.

  • http://aplosmedia.com/ Eric.Coleman

    Eric, David said nowhere in that quote that PDO was a DB-abstraction layer.

    I never said he did ;) I was responding to the portion I quoted…

  • http://www.technosailor.com Sketch

    I can see the point of using the PDO for an ODBC style database, but I fail to see the benefit in using it with MySQL. I have not run any tests, but I’m usre the built in MySQL functions (as well as the classes that use them) would be much more efficient for data handling than PDO.

  • http://aplosmedia.com/ Eric.Coleman

    Sketch, I might be wrong, but I think the PDO stuff goes all the way down to c code, i.e. using the underlying code in the mysql extension. I do -not- thing it’s built “on-top” of anything

  • pdxi

    I guess this makes DB_DataObject practically obsolete… ?

  • Lachlan

    PDO is -not- a database abstraction layer

    Why isn’t PDO a database abstraction layer? It performs the same role as other classic db abstraction layers such as ODBC and JDBC, it abstracts the mechanism for accessing a database and manipulating the returned records. Perhaps are you thinking of a query abstraction layer?

    The need for something like ADOdb or PEAR::DB is still present to provide an easier method of switching from one database to another and writing portable sql.

    In all honesty PEAR:DB and ADOdb don’t do much in the way of query abstraction either, writing portable SQL is still largely the job of the programmer.

  • andreask2
  • Pingback: Unofficial DreamHost Blog » Blog Archive » PHP Data Object support

  • Anonymous

    ggfhg

    e>

  • PDO – like JDBC?

    i got the point from PDO, and i think the developers want to make something like JDBC, it’s good, but could you tell me the procedure of this way. like how to fetch, load and return the response to know if this way is good or not

    Best Regards
    (nice step david)

  • blonkm

    Although there are already some PHP database abstraction classes available, the advantage of PDO

    Now this is definitely implying in covert language that PDO is an abstraction layer. Just wanted to make that clear.

    Then go on and check http://www.php.net/pdo where PDO is under the category ‘abstraction layers’.

  • bocrold

    trocdronp