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.

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

Gavin Sherry

PC World
Comments are now closed.

Latest News Articles

Most Popular Articles

Follow Us

GGG Evaluation Team

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.

Steph Mundell

LIFEBOOK UH574

The Fujitsu LifeBook UH574 allowed for great mobility without being obnoxiously heavy or clunky. Its twelve hours of battery life did not disappoint.

Andrew Mitsi

STYLISTIC Q702

The screen was particularly good. It is bright and visible from most angles, however heat is an issue, particularly around the Windows button on the front, and on the back where the battery housing is located.

Simon Harriott

STYLISTIC Q702

My first impression after unboxing the Q702 is that it is a nice looking unit. Styling is somewhat minimalist but very effective. The tablet part, once detached, has a nice weight, and no buttons or switches are located in awkward or intrusive positions.

Resources

Best Deals on GoodGearGuide

Compare & Save

Deals powered by WhistleOut
WhistleOut

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?