PHP

A popular web scripting language with support for PostgreSQL

PHP is a popular web scripting language which provides support for PostgreSQL, and offers a number of different connection methods.

PHP vendor-specific extension

PHP provides a selection of functions beginning with "pg_" to interact with PostgreSQL. These are not included in PHP by default, but can be enabled at compile time with the option "--with-pgsql[=DIR]".

These vendor-specific extension functions provide a quick and easy way to access PostgreSQL from PHP, but do not have any object-orientated functionality and are cumbersome to use for larger projects.

Complete function list: https://www.php.net/manual/en/ref.pgsql.php

PDO (PHP Data Objects)

In contrast to the vendor-specific database extensions, PDO provides is an abstraction layer which provides a consistent interface for accessing databases in PHP, regardless of the database backend. PDO does of course support PostgreSQL.

To install the PostgreSQL PDO extension when compiling from source, use --with-pdo-pgsql[=DIR]

PDO documentation: https://www.php.net/manual/en/book.pdo.php

ADOdb
ADOdb is a third-party database abstraction layer with support for a wide range of databases including PostgreSQL. Similar in syntax to Microsoft's "ActiveX Data Objects" (ADO), it is optimised for speed.
Website: http://adodb.org/dokuwiki/doku.php

Examples

Simple example using built-in functions:

<?php

// Create database connection
$dbconn = pg_connect("host=localhost dbname=testdb user=testuser")
    or die('Could not connect: ' . pg_last_error());

// Execute SQL query
$query = "SELECT 'Hello world'";
$result = pg_query($query) or die('Query failed: ' . pg_last_error());

// Output result
$row = pg_fetch_array($result, null);

printf("%s\n", $row[0]);

// Free resultset
pg_free_result($result);

// Close connection
pg_close($dbconn);

?>

Categories

Application interface