主頁 > 知識庫 > PHP操作Postgresql封裝類與應用完整實例

PHP操作Postgresql封裝類與應用完整實例

熱門標簽:福建銀行智能外呼系統(tǒng)價格 寧波外呼營銷系統(tǒng) 四川保險智能外呼系統(tǒng)供應商 電話機器人銷售主要負責什么 遼寧ai電銷機器人價格 長沙做地圖標注公司 上海做外呼線路的通信公司 地圖標注專員怎么樣 房產(chǎn)中介用的是什么外呼系統(tǒng)

本文實例講述了PHP操作Postgresql封裝類與應用。分享給大家供大家參考,具體如下:

這個類封裝了一些常用的函數(shù),原帖里面還有事務處理的內(nèi)容,以后再學習吧。

類文件定義:

?php
class pgsql {
private $linkid; // PostgreSQL連接標識符
private $host; // PostgreSQL服務器主機
private $port; // PostgreSQL服務器主機端口
private $user; // PostgreSQL用戶
private $passwd; // PostgreSQL密碼
private $db; // Postgresql數(shù)據(jù)庫
private $result; // 查詢的結(jié)果
private $querycount; // 已執(zhí)行的查詢總數(shù)
/* 類構(gòu)造函數(shù),用來初始化$host、$user、$passwd和$db字段。 */
function __construct($host, $port ,$db, $user, $passwd) {
$this->host = $host;
$this->port = $port;
$this->user = $user;
$this->passwd = $passwd;
$this->db = $db;
}
/* 連接Postgresql數(shù)據(jù)庫 */
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());
}
}
/* 執(zhí)行數(shù)據(jù)庫查詢。 */
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;
}
/* 確定受查詢所影響的行的總計。 */
function affectedRows(){
$count = @pg_affected_rows($this->linkid);
return $count;
}
/* 確定查詢返回的行的總計。 */
function numRows(){
$count = @pg_num_rows($this->result);
return $count;
}
/* 將查詢的結(jié)果行作為一個對象返回。 */
function fetchObject(){
$row = @pg_fetch_object($this->result);
return $row;
}
/* 將查詢的結(jié)果行作為一個索引數(shù)組返回。 */
function fetchRow(){
$row = @pg_fetch_row($this->result);
return $row;
}
/* 將查詢的結(jié)果行作為一個關聯(lián)數(shù)組返回。 */
function fetchArray(){
$row = @pg_fetch_array($this->result);
return $row;
}
/* 返回在這個對象的生存期內(nèi)執(zhí)行的查詢總數(shù)。這不是必須的,但是您也許會感興趣。 */
function numQueries(){
return $this->querycount;
}
}
?>

測試的php一并放出,另外測試了下局域網(wǎng)內(nèi)的另一臺postgresql服務器,感覺查詢速度還是很快的,查詢postgregis數(shù)據(jù)也是杠杠滴。

?php
  include 'PGDB.php';
  $PG = new pgsql("192.168.1.167", "5432", "postgis", "postgres", "post");
  $PG->connect();
  if(!$PG)
  {
    $db_error = "無法連接到PostGreSQL數(shù)據(jù)庫!";
    echo $db_error;
  }
  else
  {
    echo "成功連接!";
    $query = "select name from ex where gid = 2";
    $result = $PG->query($query);
    $row = $PG->fetchRow();
    echo $row[0];
  }
?>

更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《php+Oracle數(shù)據(jù)庫程序設計技巧總結(jié)》、《PHP+MongoDB數(shù)據(jù)庫操作技巧大全》、《php面向?qū)ο蟪绦蛟O計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》

希望本文所述對大家PHP程序設計有所幫助。

您可能感興趣的文章:
  • PostgreSQL管理工具phpPgAdmin入門指南
  • php連接與操作PostgreSQL數(shù)據(jù)庫的方法
  • PHP 讀取Postgresql中的數(shù)組
  • PHP實現(xiàn)基于PDO擴展連接PostgreSQL對象關系數(shù)據(jù)庫示例
  • PHP實現(xiàn)從PostgreSQL數(shù)據(jù)庫檢索數(shù)據(jù)分頁顯示及根據(jù)條件查找數(shù)據(jù)示例
  • PHP5中使用PDO連接數(shù)據(jù)庫的方法
  • PHP PDO函數(shù)庫詳解
  • PHP連接及操作PostgreSQL數(shù)據(jù)庫的方法詳解

標簽:宿遷 延安 澳門 宜春 常德 深圳 佛山 工商登記

巨人網(wǎng)絡通訊聲明:本文標題《PHP操作Postgresql封裝類與應用完整實例》,本文關鍵詞  PHP,操作,Postgresql,封裝,類,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《PHP操作Postgresql封裝類與應用完整實例》相關的同類信息!
  • 本頁收集關于PHP操作Postgresql封裝類與應用完整實例的相關信息資訊供網(wǎng)民參考!
  • 推薦文章