Complete example of PHP operation Postgresql encapsulation class and application

  • 2021-09-24 21:44:23
  • OfStack

This paper describes the PHP operation Postgresql encapsulation class and its application. Share it for your reference, as follows:

This class encapsulates a number of commonly used functions, the original post there are transactions in the content, later to learn it.

Class file definition:


<?php
class pgsql {
private $linkid; // PostgreSQL Connection identifier 
private $host; // PostgreSQL Server host 
private $port; // PostgreSQL Server host port 
private $user; // PostgreSQL Users 
private $passwd; // PostgreSQL Password 
private $db; // Postgresql Database 
private $result; //  Results of the query 
private $querycount; //  Total number of queries executed 
/*  Class constructor, which is used to initialize the $host , $user , $passwd And $db Field.  */
function __construct($host, $port ,$db, $user, $passwd) {
$this->host = $host;
$this->port = $port;
$this->user = $user;
$this->passwd = $passwd;
$this->db = $db;
}
/*  Connect Postgresql Database  */
function connect(){
try{
$this->linkid = @pg_connect("host=$this->host port=$this->port dbname=$this->db
user=$this->user password=$this->passwd");
if (! $this->linkid)
throw new Exception("Could not connect to PostgreSQL server.");
}
catch (Exception $e) {
die($e->getMessage());
}
}
/*  Execute a database query.  */
function query($query){
try{
$this->result = @pg_query($this->linkid,$query);
if(! $this->result)
throw new Exception("The database query failed.");
}
catch (Exception $e){
echo $e->getMessage();
}
$this->querycount++;
return $this->result;
}
/*  Determines the total number of rows affected by the query.  */
function affectedRows(){
$count = @pg_affected_rows($this->linkid);
return $count;
}
/*  Determines the total number of rows returned by the query.  */
function numRows(){
$count = @pg_num_rows($this->result);
return $count;
}
/*  Use the result row of the query as the 1 Objects are returned.  */
function fetchObject(){
$row = @pg_fetch_object($this->result);
return $row;
}
/*  Use the result row of the query as the 1 An index array is returned.  */
function fetchRow(){
$row = @pg_fetch_row($this->result);
return $row;
}
/*  Use the result row of the query as the 1 Returns an associative array.  */
function fetchArray(){
$row = @pg_fetch_array($this->result);
return $row;
}
/*  Returns the total number of queries executed during the lifetime of this object. This is not necessary, but it may interest you.  */
function numQueries(){
return $this->querycount;
}
}
?>

The test of php1 and released, in addition to testing another LAN postgresql server, feel the query speed is very fast, query postgregis data is also a bar drop.


<?php
  include 'PGDB.php';
  $PG = new pgsql("192.168.1.167", "5432", "postgis", "postgres", "post");
  $PG->connect();
  if(!$PG)
  {
    $db_error = " Unable to connect to PostGreSQL Database! ";
    echo $db_error;
  }
  else
  {
    echo " Successful connection! ";
    $query = "select name from ex where gid = 2";
    $result = $PG->query($query);
    $row = $PG->fetchRow();
    echo $row[0];
  }
?>

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", "Summary of php String (string) Usage", "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: