日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

php postgresql多条,PHP操作Postgresql封装类与应用完整实例

發布時間:2025/4/16 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php postgresql多条,PHP操作Postgresql封装类与应用完整实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

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

類文件定義:

class pgsql {

private $linkid; // PostgreSQL連接標識符

private $host; // PostgreSQL服務器主機

private $port; // PostgreSQL服務器主機端口

private $user; // PostgreSQL用戶

private $passwd; // PostgreSQL密碼

private $db; // Postgresql數據庫

private $result; // 查詢的結果

private $querycount; // 已執行的查詢總數

/* 類構造函數,用來初始化$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數據庫 */

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());

}

}

/* 執行數據庫查詢。 */

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;

}

/* 將查詢的結果行作為一個對象返回。 */

function fetchObject(){

$row = @pg_fetch_object($this->result);

return $row;

}

/* 將查詢的結果行作為一個索引數組返回。 */

function fetchRow(){

$row = @pg_fetch_row($this->result);

return $row;

}

/* 將查詢的結果行作為一個關聯數組返回。 */

function fetchArray(){

$row = @pg_fetch_array($this->result);

return $row;

}

/* 返回在這個對象的生存期內執行的查詢總數。這不是必須的,但是您也許會感興趣。 */

function numQueries(){

return $this->querycount;

}

}

?>

測試的php一并放出,另外測試了下局域網內的另一臺postgresql服務器,感覺查詢速度還是很快的,查詢postgregis數據也是杠杠滴。

include 'PGDB.php';

$PG = new pgsql("192.168.1.167", "5432", "postgis", "postgres", "post");

$PG->connect();

if(!$PG)

{

$db_error = "無法連接到PostGreSQL數據庫!";

echo $db_error;

}

else

{

echo "成功連接!";

$query = "select name from ex where gid = 2";

$result = $PG->query($query);

$row = $PG->fetchRow();

echo $row[0];

}

?>

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

總結

以上是生活随笔為你收集整理的php postgresql多条,PHP操作Postgresql封装类与应用完整实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。