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 PC World newsletter!

Error: Please check your email address.

Our Back to Business guide highlights the best products for you to boost your productivity at home, on the road, at the office, or in the classroom.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Essentials

Microsoft L5V-00027 Sculpt Ergonomic Keyboard Desktop

Learn more >

Lexar® JumpDrive® S57 USB 3.0 flash drive

Learn more >

Mobile

Lexar® JumpDrive® S45 USB 3.0 flash drive 

Learn more >

Exec

Audio-Technica ATH-ANC70 Noise Cancelling Headphones

Learn more >

HD Pan/Tilt Wi-Fi Camera with Night Vision NC450

Learn more >

Lexar® JumpDrive® C20c USB Type-C flash drive 

Learn more >

Lexar® Professional 1800x microSDHC™/microSDXC™ UHS-II cards 

Learn more >

Budget

Back To Business Guide

Click for more ›

Most Popular Reviews

Latest News Articles

Resources

GGG Evaluation Team

Michael Hargreaves

Windows 10 for Business / Dell XPS

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Aysha Strobbe

Windows 10 / HP Spectre

Ultimately, I think the Windows 10 environment is excellent for me as it caters for so many different uses. The inclusion of the Xbox app is also great for when you need some downtime too!

Mark Escubio

Windows 10 / Lenovo Yoga

For me, the Xbox Play Anywhere is a great new feature as it allows you to play your current Xbox games with higher resolutions and better graphics without forking out extra cash for another copy. Although available titles are still scarce, but I’m sure it will grow in time.

Kathy Cassidy

STYLISTIC Q702

First impression on unpacking the Q702 test unit was the solid feel and clean, minimalist styling.

Anthony Grifoni

STYLISTIC Q572

For work use, Microsoft Word and Excel programs pre-installed on the device are adequate for preparing short documents.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?