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

Most Popular Reviews

Latest News Articles

Resources

PCW Evaluation Team

Azadeh Williams

HP OfficeJet Pro 8730

A smarter way to print for busy small business owners, combining speedy printing with scanning and copying, making it easier to produce high quality documents and images at a touch of a button.

Andrew Grant

HP OfficeJet Pro 8730

I've had a multifunction printer in the office going on 10 years now. It was a neat bit of kit back in the day -- print, copy, scan, fax -- when printing over WiFi felt a bit like magic. It’s seen better days though and an upgrade’s well overdue. This HP OfficeJet Pro 8730 looks like it ticks all the same boxes: print, copy, scan, and fax. (Really? Does anyone fax anything any more? I guess it's good to know the facility’s there, just in case.) Printing over WiFi is more-or- less standard these days.

Ed Dawson

HP OfficeJet Pro 8730

As a freelance writer who is always on the go, I like my technology to be both efficient and effective so I can do my job well. The HP OfficeJet Pro 8730 Inkjet Printer ticks all the boxes in terms of form factor, performance and user interface.

Michael Hargreaves

Windows 10 for Business / Dell XPS 13

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 x360

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 910

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.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?