PostgreSQ PHP  | July 2013

DATA SERVER | ORDBMS | PRODUCTION | Gene Casanova, Senior Database Developer | EXPERIMENTAL



Bookmark and Share
DATABASE MIGRATION SERVICES | DATA ENTRY | DATA PROCESSING PROGRAMMING | CROSS-PLATFORM INTEGRATION
INTERNET / EXTRANET / INTRANET - BACKEND/ FRONTEND WEB APP/WEB SITE/E-COMMERCE DEV
SMALL BUSINESS / MEDIUM BUSINESS DATABASE ADMINSTRATION | ENTERPRICE DATABASE DEVELOP & DEPLOYMENT SOFTWARE & HARDWARE
CUSTOM BUILD WORLD-STANDARD HARDWARE COMMERCIAL-INTERNET & BUSINESS CLASS SERVERS & OEM PARTS

PostgreSQL Database PostgreSQL-Server & PHP

PostgreSQL, the object-relational database management system (ORDBMS).

The key is 'database management'. Keep this key in mind when developing processes for the system, when working with data and this system, and when developing applications that work with databases hosted by this system.

This document presents examples to illustrate basic database interaction functionality using PHP processing for web application.  Connect, Query, Display Results, and Disconnect from a PostgreSQL database is covered.

PHP-PostgreSQL functionality works with PostgreSQL 6.5 or later.  PostgreSQL 8.0 or later is required to enable using PHP-PostgreSQL module features.

Notice, PostgreSQL supports many character-encodings including multibyte character encoding.

PHP Runtime Configuration

The behaviour of the following PHP-functions is affected by settings in the "php.ini" file.

Database (Data Server)

Start with a working PostgreSQL-server hosting a database.  Review: the 'database'; a store for data.  This document was written based on PosthreSQL-server current-release in July 2013.

Example

The following process was created to illustrate connecting, querying, displaying results, and disconnecting from a PostgreSQL database using PHP web preprocessing.  To run this process, have the standard PostgreSQL-server installation with the initial database running.  Have a PostgreSQL-server user-account "www" established with a password.

<?php

// Connect To A PostgreSQL Server And Select Database To Access:
$dbconn = pg_connect("host=localhost dbname=test user=www password=keepthissecured")
or die('Could Not Connect: ' . pg_last_error());

// Performing SQL Query:
$query = 'SELECT * FROM authors';
$result = pg_query($query) or die('Query Failed: ' . pg_last_error());

// Output Results To A HTML Table:
echo "<table>\n";
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";

// Free Resultset:
pg_free_result($result);

// Close Connection To Database Server:
pg_close($dbconn);
?>

Notice, this example code was written to illustrate the basic processes; not to be production code in a seriously-deployed commercial application.


Intended For Internal Private Use.  Use At Your Own Risk. ©2013 Copyright Gene Casanova.