PHP and PostgreSQL

This column has been looking at ways to simplify the development of a database-driven Web-based address book. Now we look at a complete solution: the PostgreSQL Object Relational Database Management System (ORDBMS).

Python vs. PHP: Choosing your next project's language

The Structured Query Language (SQL) was developed to be a language-driven interface to data management. The most recent standard is SQL99. SQL allows database users to treat data in terms of set theory, which is a school of mathematics focusing on grouping data into sets and building relationships between these sets.

PostgreSQL

This is a leading open source database that supports much of SQL99. The PostgreSQL source code can be downloaded from http://www.postgresql.org. You will need access to a UNIX machine (Linux, BSD, Solaris, etc.) to install it. The installation process is described in detail in the 'INSTALL' file in the top level directory of the source tree.

Once you have installed PostgreSQL, you will need to recompile PHP with PostgreSQL support. For more information, see the INSTALL file in the top level directory of the PHP source tree as well as the output of ./configure --help.

Creating A Database

Once you have installed PostgreSQL, log in to your UNIX system as the database superuser. Then type:

$ /usr/local/pgsql/bin/psql template1
...
template1=#

Where '$' is the shell prompt and 'template1=#' is an SQL prompt. This will connect you to the pre-existing PostgreSQL database. You can then create another database.

template1=# CREATE DATABASE phphh;
CREATE DATABASE
template1=# \c phphh
phphh=#

On the first line of input, we create the database 'phphh'. The second line is feedback from the database backend that the database was correctly created. The third line sees us connecting to the new database with the \c connect command (note that this third line is not SQL, it is a command to the psql program we are using). The fourth line shows us connected to the new database.

Creating A Table

SQL databases store data in tables or 'relations'. If we wanted to create a table called 'test' with three columns a, b and c, we could do the following:

phphh=# CREATE TABLE test ( a int, b text, c text);
CREATE
phphh=#

We now have a table in which we can insert, update, delete and retrieve data.

Inserting Data

This is done via INSERT. An example follows:

phphh=# INSERT INTO test VALUES(1,'test1','test2');
INSERT 50441 1
phphh=#

The line 'INSERT 50441 1' indicates that the row was correctly inserted, that it is the 50441st insert and that a single row ('1') was inserted.

Retrieving Data

This is done via SELECT. An example follows:

phphh=# SELECT * FROM test;
a | b | c
---+-------+-------
1 | test1 | test2
(1 row)

phphh=#

Using SELECT, we are able to retrieve the data we just inserted. Notice how this parallels the concept of the spreadsheet.

Deleting Data

Data is removed from a table with a DELETE query. The DELETE syntax resembles that of SELECT:

phphh=# DELETE FROM test;
DELETE 1
phphh=# 

Use a SELECT to verify that the data has been removed.

Updating Rows

Updating a table is, in effect, a DELETE followed by a SELECT. This is simplified in SQL as UPDATE.

phphh=# INSERT INTO test VALUES(1,'test1','test2');
INSERT 50442 1
phphh=# UPDATE test SET a=2; UPDATE 1
phphh=# SELECT * FROM test;
a | b | c
---+-------+-------
2 | test1 | test2
(1 row)

phphh=#

Notice that the value of the column 'a' has been changed from 1 to 2.

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.

More about ----ASE ITHewlett-Packard AustraliaHPLinux

Show Comments
[]