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


Posted in PHP onApril 24, 2018

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

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

类文件定义:

<?php
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数据也是杠杠滴。

<?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 相关文章推荐
Windows Apache2.2.11及Php5.2.9-1的安装与配置方法
Jun 08 PHP
PHP iconv 函数转gb2312的bug解决方法
Oct 11 PHP
按上下级层次关系输出内容的PHP代码
Jul 17 PHP
PHP中英混合字符串截取函数代码
Jul 17 PHP
php中使用Curl、socket、file_get_contents三种方法POST提交数据
Aug 12 PHP
php循环语句 for()与foreach()用法区别介绍
Sep 05 PHP
基于header的一些常用指令详解
Jun 06 PHP
PHP实现根据浏览器跳转不同语言页面代码
Aug 02 PHP
PHP递归调用数组值并用其执行指定函数的方法
Apr 01 PHP
微信公众平台开发之天气预报功能
Aug 31 PHP
比较完整的微信开发php代码
Aug 02 PHP
在laravel中实现ORM模型使用第二个数据库设置
Oct 24 PHP
php接口实现拖拽排序功能
Apr 23 #PHP
PHP基于SPL实现的迭代器模式示例
Apr 22 #PHP
PHP生成推广海报的方法分享
Apr 22 #PHP
PHP排序算法之归并排序(Merging Sort)实例详解
Apr 21 #PHP
PHP排序算法之快速排序(Quick Sort)及其优化算法详解
Apr 21 #PHP
Laravel模型间关系设置分表的方法示例
Apr 21 #PHP
PHP排序算法之基数排序(Radix Sort)实例详解
Apr 21 #PHP
You might like
PHP实现全角字符转为半角方法汇总
2015/07/09 PHP
PHP入门教程之会话控制技巧(cookie与session)
2016/09/11 PHP
PHP正则匹配操作简单示例【preg_match_all应用】
2017/07/10 PHP
Laravel如何实现适合Api的异常处理响应格式
2020/06/14 PHP
Javascript 兼容firefox的一些问题
2009/05/21 Javascript
模仿JQuery sortable效果 代码有错但值得看看
2009/11/05 Javascript
基于json的jquery地区联动效果代码
2011/07/06 Javascript
用nodejs实现PHP的print_r函数代码
2014/03/14 NodeJs
angularJS 入门基础
2015/02/09 Javascript
js实现双击图片放大单击缩小的方法
2015/02/17 Javascript
JavaScript里实用的原生API汇总
2015/05/14 Javascript
浅谈javascript原型链与继承
2015/07/13 Javascript
20分钟打造属于你的Bootstrap站点
2016/07/27 Javascript
vue2.0多条件搜索组件使用详解
2020/03/26 Javascript
vue加载自定义的js文件方法
2018/03/13 Javascript
node.js中TCP Socket多进程间的消息推送示例详解
2018/07/10 Javascript
vue-cli项目中使用echarts图表实例
2018/10/22 Javascript
javascript实现导航栏分页效果
2019/06/27 Javascript
javascript中的相等操作符(==与===区别)
2019/12/21 Javascript
js面向对象之实现淘宝放大镜
2020/01/15 Javascript
python正则表达式修复网站文章字体不统一的解决方法
2013/02/21 Python
一道python走迷宫算法题
2018/01/22 Python
浅谈Pytorch中的torch.gather函数的含义
2019/08/18 Python
Python 之 Json序列化嵌套类方式
2020/02/27 Python
Python itertools.product方法代码实例
2020/03/27 Python
keras中的卷积层&amp;池化层的用法
2020/05/22 Python
python中如何写类
2020/06/29 Python
python输出结果刷新及进度条的实现操作
2020/07/13 Python
丝芙兰中国官方商城:SEPHORA中国
2018/01/10 全球购物
沙特阿拉伯排名第一的在线时尚购物应用程序:1Zillion
2020/08/08 全球购物
Net Remoting把服务器端激活两种模式
2014/01/22 面试题
大专生自我鉴定范文
2013/10/01 职场文书
教学实习自我评价
2014/01/28 职场文书
文化建设工作方案
2014/05/12 职场文书
Jmerte 分布式压测及分布式压测配置
2022/04/30 Java/Android
python 判断字符串当中是否包含字符(str.contain)
2022/06/01 Python