Detailed Explanation of PHP Connection and Operation of PostgreSQL Database

  • 2021-11-14 05:13:13
  • OfStack

This paper describes the method of connecting PHP and operating PostgreSQL database with examples. Share it for your reference, as follows:

The PostgreSQL extension is enabled by default in the latest version of PHP 5.3. x. You can disable it at compile time using--without-pgsql. You can still install the PHP-PostgreSQL interface using the yum command:


yum install php-pgsql

Before you begin using PHP to connect to the PostgreSQL interface, locate the pg_hba. conf file in the PostgreSQL installation directory and add the following line:


# IPv4 local connections:
host  all     all     127.0.0.1/32     md5

You can start/restart the postgres server using the following command:


[root@host]# service postgresql restart
Stopping postgresql service:                [ OK ]
Starting postgresql service:                [ OK ]

Windows users must enable php_pgsql. dll to use this extension. This DLL is included in the Windows release in the latest version of PHP 5.3. x.

PHP Connect to PostgreSQL Database

The following PHP code shows how to connect to an existing database on the local machine, and finally returns the database connection object.


<?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";
  }
?>

Now, let's run the above program to open the database: testdb. If the database connection is successfully opened, it will give the following message:

Opened database successfully

Create a table

The following PHP program will be used to create 1 table in the database (testdb) created earlier:


<?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
   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 you execute the above program, it creates an COMPANY table in the testdb database and displays the following message:

Opened database successfully
Table created successfully

SQL

Insert operation

The following PHP program shows how to create records in the COMPANY table created in the 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 you execute the above program, it creates the given record in the COMPANY table and displays the following two rows:

Opened database successfully
Records created successfully

SELECT operation

The following PHP program shows how to get and display records from the COMPANY table created in the 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
   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);
?>

When the above program is executed, the following results will be produced. Note that the fields are returned in the order in which they are used when creating tables.

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

The following PHP code shows how to use the UPDATE statement to update the specified record, and then get and display the updated record from the COMPANY table:


<?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
   UPDATE COMPANY set SALARY = 25000.00 where ID=1;
EOF;
  $ret = pg_query($db, $sql);
  if(!$ret){
   echo pg_last_error($db);
   exit;
  } else {
   echo "Record updated successfully\n";
  }
  $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);
?>

When the above procedure is executed, the following results will be produced:

Opened database successfully
Record updated successfully
ID = 2
NAME = Allen
ADDRESS = 25
SALARY = 15000
ID = 3
NAME = Teddy
ADDRESS = 23
SALARY = 20000
ID = 4
NAME = Mark
ADDRESS = 25
SALARY = 65000
ID = 1
NAME = Paul
ADDRESS = 32
SALARY = 25000
Operation done successfully

Delete operation

The following PHP code shows how to use the DELETE statement to delete a specified record, and then get and display the remaining records from the COMPANY table:


<?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
   DELETE from COMPANY where ID=2;
EOF;
  $ret = pg_query($db, $sql);
  if(!$ret){
   echo pg_last_error($db);
   exit;
  } else {
   echo "Record deleted successfully\n";
  }
  $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);
?>

When the above procedure is executed, the following results will be produced:

Opened database successfully
Record deleted successfully
ID = 3
NAME = Teddy
ADDRESS = 23
SALARY = 20000
ID = 4
NAME = Mark
ADDRESS = 25
SALARY = 65000
ID = 1
NAME = Paul
ADDRESS = 32
SALARY = 25000
Operation done successfully

For more readers interested in PHP related contents, please check the topics on this site: "Summary of PHP Database Operation Skills Based on pdo", "Summary of php+Oracle Database Programming Skills", "Encyclopedia of PHP+MongoDB Database Operation Skills", "Introduction to php Object-Oriented Programming", "Usage Summary of php String (string)", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: