PHP Operation PostgreSQL Package class and application complete instance

Source: Internet
Author: User
This article mainly introduces the PHP operation PostgreSQL Package class, combined with instance form analysis of PHP for PostgreSQL database Common connection, query, statistics and other operations packaging techniques and use, the need for friends can refer to the following

The examples in this paper describe the PHP operation PostgreSQL encapsulation class and application. Share to everyone for your reference, as follows:

This class encapsulates a number of commonly used functions, the original post has the content of transaction processing, and then learn it.

class file Definition:


<?phpclass Pgsql {private $linkid;//PostgreSQL connection identifier private $host;//PostgreSQL server host private $port;//PostgreSQL Server Master Machine Port private $user; PostgreSQL user private $passwd; PostgreSQL Password Private $db; PostgreSQL database Private $result; The result of the query is private $querycount; Total number of queries executed/* class constructors, which are used to initialize $host, $user, $passwd, and $db fields.  */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->dbuser= $this->user password= $this->passwd "), if (! $this->linkid) throw new Exception (" Could not connect to PostgreSQL server. "); catch (Exception $e) {die ($e->getmessage ());}} /* Execute 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 of rows affected by the query. */function affectedrows () {$count = @pg_affected_rows ($this->linkid); return $count;} /* Determines the total of rows returned by the query. */function NumRows () {$count = @pg_num_rows ($this->result); return $count;} /* Returns the result row of the query as an object. */function Fetchobject () {$row = @pg_fetch_object ($this->result); return $row;} /* Returns the result row of the query as an indexed array. */function Fetchrow () {$row = @pg_fetch_row ($this->result); return $row;} /* Returns the result row of the query as an associative array. */function Fetcharray () {$row = @pg_fetch_array ($this->result); return $row;} /* Returns the total number of queries executed over the lifetime of this object. This is not necessary, but you may be interested. */function numqueries () {return $this->querycount;}}? >


Test the PHP to release, in addition to test the next LAN within the other PostgreSQL server, the feeling of query speed is very fast, query Postgregis data is also a leverage drop.


<?php  include ' pgdb.php ';  $PG = new Pgsql ("192.168.1.167", "5432", "PostGIS", "Postgres", "post");  $PG->connect ();  if (! $PG)  {    $db _error = "Cannot connect to the PostgreSQL database! ";    echo $db _error;  }  else  {    echo "successfully connected! ";    $query = "SELECT name from ex where GID = 2";    $result = $PG->query ($query);    $row = $PG->fetchrow ();    echo $row [0];  }? >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.