Question

I've been using PHP & MySQL for ages and am about to start using PostgreSQL instead.

What's the preferred method?

Is it via the PDO objects or is there something better?

Was it helpful?

Solution

PDO objects are the new hotness. I'd recommend that as long as you can ensure that your target platform will always be running PHP 5.2+.

There are many other database abstraction layers that support PostgreSQL that are compatible with older versions of PHP; I'd recommend ADODB.

You should really be using PDO or a different abstraction layer even for your MySQL work; that way you won't have this problem again!

OTHER TIPS

Using Zend Db:

require_once 'Zend/Db.php';
$DB_ADAPTER = 'Pdo_Pgsql';
$DB_CONFIG = array(
    'username' => 'app_db_user',
    'password' => 'xxxxxxxxx',
    'host'     => 'localhost',
    'port'     => 5432,
    'dbname'   => 'mydb'
);
$db = Zend_Db::factory($DB_ADAPTER, $DB_CONFIG);

I, personally, use PDO for all my database work when I have the choice. Prepared statements make my life easy, and it is seamless between database systems - handy if you have to work with one you're not used to.

If you want to roll your own abstraction, or go with the procedural model, here's the Postgre functions: http://ca.php.net/manual/en/ref.pgsql.php

There are also the pg_whatever functions, but don't use them.

They use older, unmaintained database drivers. PDO is the way to go.

I would also suggest creating an inherited PDO class or a wrapper class if you decide not to use PDO. This would provide you with a lot more flexibility in the future. ie. Calculating query execution time.

Depending on the scale of your application, you might wish to consider the number of connections going to the backend. The consensus seem to be that PHP persistent connections and PostgreSQL don't work well together, so something like pgpool-|| should be used as an intermediary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top