Postgresql PHP PDF
Postgresql PHP PDF
Postgresql PHP PDF
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:
Before you start using PHP PostgreSQL interface, find pg_hba.conf file in your PostgreSQL
installation directory and add the following line:
You can start/restart postgres server in case it is not running using the following command:
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.
This routine resets the connection. It is useful for error recovery. Returns TRUE on
success or FALSE on failure.
This routine returns the name of the database that the given PostgreSQL connection
resource.
5 resource pg_prepare [resource$connection], string$stmtname, string$query
This submits a request to create a prepared statement with the given parameters and
waits for completion.
This routine sends a request to execute a prepared statement with given parameters and
waits for the result.
This routine fetches one row of data from the result associated with the specified result
resource.
This routine returns an array that contains all rows records in the result resource.
This routine returns the number of rows affected by INSERT, UPDATE, and DELETE
queries.
This routine returns the number of rows in a PostgreSQL result resource for example
number of rows returned by SELECT statement.
This routine returns the last error message for a given connection.
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 = "host=127.0.0.1";
$port = "port=5432";
$dbname = "dbname=testdb";
$credentials = "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:
Create a Table
Following PHP program will be used to create a table in previously created database:
<?php
$host = "host=127.0.0.1";
$port = "port=5432";
$dbname = "dbname=testdb";
$credentials = "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;
When above program is executed, it will create COMPANY table in your testdb and it will display
the following messages:
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";
$sql =<<<EOF
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Paul', 32, 'California', 20000.00 );
When above program is executed, it will create given records in COMPANY table and will display
the following two lines:
SELECT Operation
Following PHP program shows how we can fetch and display records from 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";
$sql =<<<EOF
SELECT * from COMPANY;
EOF;
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.
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
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 = "host=127.0.0.1";
$port = "port=5432";
$dbname = "dbname=testdb";
$credentials = "user=postgres password=pass123";
$sql =<<<EOF
SELECT * from COMPANY;
EOF;
ID = 3
NAME = Teddy
ADDRESS = 23
SALARY = 20000
ID = 4
NAME = Mark
ADDRESS = 25
SALARY = 65000
ID = 1
NAME = Paul
ADDRESS = 32
SALARY = 25000
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 = "host=127.0.0.1";
$port = "port=5432";
$dbname = "dbname=testdb";
$credentials = "user=postgres password=pass123";
ID = 4
NAME = Mark
ADDRESS = 25
SALARY = 65000
ID = 1
NAME = Paul
ADDRESS = 32
SALARY = 25000