PHP, Pear MDB2 and PostgreSQL

Did you ever wonder how to write portable PHP code that works on MySQL, PostgreSQL, Oracle, MSSQL or SQLite? Then what you need is the Pear MDB2 package. It’s an abstraction layer between your PHP code and the database. It’s really not hard to learn and use! Of course, you have to use “standard” SQL queries that are common to all databases in your PHP code :)

In Fedora 8/9, you can install the MDB2 package with yum install php-pear-MDB2. This is the base package, now you need to install a driver for your database. To access MySQL it’s yum install php-pear-Driver-mysqli.

Note that on the screenshot above (PackageKit in F9), there are only 2 MySQL drivers available as RPM’s in Fedora. This little choice defeats the purpose of the abstraction layer, so I’m probably going to create driver packages for the other databases (or at least PostgreSQL) next week and try to push them into Fedora.

7 Responses to “PHP, Pear MDB2 and PostgreSQL”

  1. Scott Baker Says:

    What makes MDB2 any different than PDO? PDO comes pre-compiled in PHP and would most likely be quicker to get up an running and running than a PEAR module.

  2. Steven Says:

    There is no big difference as far as I know, but I usually prefer pear modules as they are really easy to install and upgrade. And besides that, PDO needs to be compiled with PHP, it’s less flexible.

    If you don’t want to use the RPM’s for pear, you can install all the above modules just by typing:
    $ pear install MDB2
    $ pear install MDB2#mysql
    $ pear install MDB2#pgsql

    Quick, easy, clean. But you are right, it’s a matter of choice.

  3. juankprada Says:

    I wonder if you have ever heard of Zend Framework, using Zend_Db class you have the option to connect to any of those DBs and without using any SQL code which makes it even easier to write portable php code, you just define the type of db in a config file and set the right one to be used in the application

  4. Steven Says:

    Hi, I never used PHP frameworks because my usage is pretty basic :) MDB2 was simply a way to make a simple PHP code better without learning anything hard.

  5. juankprada Says:

    maybe its just me…. but i find using Zend Framework easier than raw php
    you should give it a try. It’s not that difficult and you will find yourself spending less time on repetitive code. =)

  6. joe Says:

    Do you know how to install XML_HTMLSax package?
    thanks.

    as I tried
    # pear install XML_HTMLSax
    No releases available for package “pear.php.net/XML_HTMLSax”
    Cannot initialize ‘channel://pear.php.net/XML_HTMLSax’, invalid or missing package file
    Package “channel://pear.php.net/XML_HTMLSax” is not valid
    install failed

  7. Steven Says:

    Sorry but I don’t know. Btw, this looks like an old package that you shouldn’t be using.

Leave a Reply