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!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Sarah Ieroianni

Brother QL-820NWB Professional Label Printer

The print quality also does not disappoint, it’s clear, bold, doesn’t smudge and the text is perfectly sized.

Ratchada Dunn

Sharp PN-40TC1 Huddle Board

The Huddle Board’s built in program; Sharp Touch Viewing software allows us to easily manipulate and edit our documents (jpegs and PDFs) all at the same time on the dashboard.

George Khoury

Sharp PN-40TC1 Huddle Board

The biggest perks for me would be that it comes with easy to use and comprehensive programs that make the collaboration process a whole lot more intuitive and organic

David Coyle

Brother PocketJet PJ-773 A4 Portable Thermal Printer

I rate the printer as a 5 out of 5 stars as it has been able to fit seamlessly into my busy and mobile lifestyle.

Kurt Hegetschweiler

Brother PocketJet PJ-773 A4 Portable Thermal Printer

It’s perfect for mobile workers. Just take it out — it’s small enough to sit anywhere — turn it on, load a sheet of paper, and start printing.

Matthew Stivala

HP OfficeJet 250 Mobile Printer

The HP OfficeJet 250 Mobile Printer is a great device that fits perfectly into my fast paced and mobile lifestyle. My first impression of the printer itself was how incredibly compact and sleek the device was.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?