PHP操作Postgresql封装类与应用完整实例

时间:2021-05-26

本文实例讲述了PHP操作Postgresql封装类与应用。分享给大家供大家参考,具体如下:

这个类封装了一些常用的函数,原帖里面还有事务处理的内容,以后再学习吧。

类文件定义:

<?phpclass pgsql {private $linkid; // PostgreSQL连接标识符private $host; // PostgreSQL服务器主机private $port; // PostgreSQL服务器主机端口private $user; // PostgreSQL用户private $passwd; // PostgreSQL密码private $db; // Postgresql数据库private $result; // 查询的结果private $querycount; // 已执行的查询总数function __construct($host, $port ,$db, $user, $passwd) {$this->host = $host;$this->port = $port;$this->user = $user;$this->passwd = $passwd;$this->db = $db;}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());}}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数据也是杠杠滴。

<?php 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基于pdo操作数据库技巧总结》、《php+Oracle数据库程序设计技巧总结》、《PHP+MongoDB数据库操作技巧大全》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章