亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP操作Postgresql封裝類與應(yīng)用完整實(shí)例

 更新時(shí)間:2018年04月24日 12:14:43   作者:HuFeiHu-Blog  
這篇文章主要介紹了PHP操作Postgresql封裝類,結(jié)合實(shí)例形式分析了php針對(duì)Postgresql數(shù)據(jù)庫常見的連接、查詢、統(tǒng)計(jì)等操作封裝技巧與使用方法,需要的朋友可以參考下

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

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

類文件定義:

<?php
class pgsql {
private $linkid; // PostgreSQL連接標(biāo)識(shí)符
private $host; // PostgreSQL服務(wù)器主機(jī)
private $port; // PostgreSQL服務(wù)器主機(jī)端口
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;
}
/* 確定受查詢所影響的行的總計(jì)。 */
function affectedRows(){
$count = @pg_affected_rows($this->linkid);
return $count;
}
/* 確定查詢返回的行的總計(jì)。 */
function numRows(){
$count = @pg_num_rows($this->result);
return $count;
}
/* 將查詢的結(jié)果行作為一個(gè)對(duì)象返回。 */
function fetchObject(){
$row = @pg_fetch_object($this->result);
return $row;
}
/* 將查詢的結(jié)果行作為一個(gè)索引數(shù)組返回。 */
function fetchRow(){
$row = @pg_fetch_row($this->result);
return $row;
}
/* 將查詢的結(jié)果行作為一個(gè)關(guān)聯(lián)數(shù)組返回。 */
function fetchArray(){
$row = @pg_fetch_array($this->result);
return $row;
}
/* 返回在這個(gè)對(duì)象的生存期內(nèi)執(zhí)行的查詢總數(shù)。這不是必須的,但是您也許會(huì)感興趣。 */
function numQueries(){
return $this->querycount;
}
}
?>

測(cè)試的php一并放出,另外測(cè)試了下局域網(wǎng)內(nèi)的另一臺(tái)postgresql服務(wù)器,感覺查詢速度還是很快的,查詢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];
  }
?>

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

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論