Postgresql - PHP Interface
Postgresql - PHP Interface
Postgresql - PHP Interface
Copyright tutorialspoint.com
http://www.tutorialspoint.com/postgresql/postgresql_php.htm
Installation
The PostgreSQL extension is enabled by default in the latest releases of PHP 5.3.x. It's possible to
disable it by using --without-pgsql at compile time. Still you can use yum command to install PHP
-PostgreSQL interface:
yum install php-pgsql
Before you start using PHP PostgreSQL interface, find pg_hba.conf file in your PostgreSQL
installation directory and add the following line:
# IPv4 local connections:
host
all
all
127.0.0.1/32
md5
You can start/restart postgres server in case it is not running using the following command:
[root@host]# service postgresql restart
Stopping postgresql service:
Starting postgresql service:
[
[
OK
OK
]
]
Windows users must enable php_pgsql.dll in order to use this extension. This DLL is included with
Windows distributions in the latest releases of PHP 5.3.x
For detailed installation instructions, kindly check our PHP tutorial and its official website.
10
11
12
13
14
15
Connecting To Database
Following PHP code shows how to connect to an existing database on a local machine and finally a
database connection object will be returned.
<?php
$host
$port
$dbname
$credentials
=
=
=
=
"host=127.0.0.1";
"port=5432";
"dbname=testdb";
"user=postgres password=pass123";
);
?>
Now, let's run above program to open our database testdb, if database is successfully opened,
then it will give the following message:
Opened database successfully
Create a Table
Following PHP program will be used to create a table in previously created database:
<?php
$host
$port
$dbname
$credentials
=
=
=
=
"host=127.0.0.1";
"port=5432";
"dbname=testdb";
"user=postgres password=pass123";
);
$sql =<<<EOF
CREATE TABLE COMPANY
(ID INT PRIMARY KEY
NOT NULL,
NAME
TEXT
NOT NULL,
AGE
INT
NOT NULL,
ADDRESS
CHAR(50),
SALARY
REAL);
EOF;
$ret = pg_query($db, $sql);
if(!$ret){
echo pg_last_error($db);
} else {
echo "Table created successfully\n";
}
pg_close($db);
?>
When above program is executed, it will create COMPANY table in your testdb and it will display
the following messages:
Opened database successfully
Table created successfully
INSERT Operation
Following PHP program shows how we can create records in our COMPANY table created in above
example:
<?php
$host
= "host=127.0.0.1";
$port
= "port=5432";
$dbname
= "dbname=testdb";
$credentials = "user=postgres password=pass123";
$db = pg_connect( "$host $port $dbname $credentials"
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
);
$sql =<<<EOF
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Paul', 32, 'California', 20000.00 );
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (2, 'Allen', 25, 'Texas', 15000.00 );
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );
EOF;
$ret = pg_query($db, $sql);
if(!$ret){
echo pg_last_error($db);
} else {
echo "Records created successfully\n";
}
pg_close($db);
?>
When above program is executed, it will create given records in COMPANY table and will display
the following two lines:
Opened database successfully
Records created successfully
SELECT Operation
Following PHP program shows how we can fetch and display records from our COMPANY table
created in above example:
<?php
$host
$port
$dbname
$credentials
=
=
=
=
"host=127.0.0.1";
"port=5432";
"dbname=testdb";
"user=postgres password=pass123";
);
echo
echo
echo
echo
}
echo "Operation done successfully\n";
pg_close($db);
?>
When above program is executed, it will produce the following result. Keep a note that fields are
returned in the sequence they were used while creating table.
Opened database successfully
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 20000
ID = 2
NAME = Allen
ADDRESS = Texas
SALARY = 15000
ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000
ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000
Operation done successfully
UPDATE Operation
Following PHP code shows how we can use UPDATE statement to update any record and then fetch
and display updated records from our COMPANY table:
<?php
$host
$port
$dbname
$credentials
=
=
=
=
"host=127.0.0.1";
"port=5432";
"dbname=testdb";
"user=postgres password=pass123";
);
if(!$ret){
echo pg_last_error($db);
exit;
}
while($row = pg_fetch_row($ret)){
echo "ID = ". $row[0] . "\n";
echo "NAME = ". $row[1] ."\n";
echo "ADDRESS = ". $row[2] ."\n";
echo "SALARY = ".$row[4] ."\n\n";
}
echo "Operation done successfully\n";
pg_close($db);
?>
DELETE Operation
Following PHP code shows how we can use DELETE statement to delete any record and then fetch
and display remaining records from our COMPANY table:
<?php
$host
$port
$dbname
$credentials
=
=
=
=
"host=127.0.0.1";
"port=5432";
"dbname=testdb";
"user=postgres password=pass123";
);
$sql =<<<EOF
SELECT * from COMPANY;
EOF;
$ret = pg_query($db, $sql);
if(!$ret){
echo pg_last_error($db);
exit;
}
while($row = pg_fetch_row($ret)){
echo "ID = ". $row[0] . "\n";
echo "NAME = ". $row[1] ."\n";
echo "ADDRESS = ". $row[2] ."\n";
echo "SALARY = ".$row[4] ."\n\n";
}
echo "Operation done successfully\n";
pg_close($db);
?>