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 相关文章推荐
缓存技术详谈―php
Dec 14 PHP
php邮件发送,php发送邮件的类
Mar 24 PHP
PHP中文分词 自动获取关键词介绍
Nov 13 PHP
解析yahoo邮件用phpmailer发送的实例
Jun 24 PHP
php模仿asp Application对象在线人数统计实现方法
Jan 04 PHP
PHP实现返回JSON和XML的类分享
Jan 28 PHP
PHP学习笔记(二):变量详解
Apr 17 PHP
如何在HTML 中嵌入 PHP 代码
May 13 PHP
php判断两个日期之间相差多少个月份的方法
Jun 18 PHP
使用JavaScript创建新样式表和新样式规则
Jun 14 PHP
php微信开发之百度天气预报
Nov 18 PHP
实现php删除链表中重复的结点
Sep 27 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异步执行的常用方式详解
2013/06/03 PHP
php将图片保存入mysql数据库失败的解决方法
2014/12/27 PHP
php处理抢购类功能的高并发请求
2018/02/08 PHP
关于JQuery($.load)事件的用法和分析
2013/04/09 Javascript
浅析js中取绝对值的2种方法
2013/07/09 Javascript
动态加载js、css等文件跨iframe实现
2014/02/24 Javascript
jQuery中对未来的元素绑定事件用bind、live or on
2014/04/17 Javascript
jquery任意位置浮动固定层插件用法实例
2015/05/29 Javascript
详解JavaScript时间格式化
2015/12/23 Javascript
基于javascript简单实现对身份证校验
2021/01/25 Javascript
mui上拉加载功能实例详解
2017/04/13 Javascript
详解vue-cli快速构建项目以及引入bootstrap、jq
2017/05/26 Javascript
jQuery初级教程之网站品牌列表效果
2017/08/02 jQuery
Vue.js 通过jQuery ajax获取数据实现更新后重新渲染页面的方法
2018/08/09 jQuery
Vue一次性简洁明了引入所有公共组件的方法
2018/11/28 Javascript
jQuery对底部导航进行跳转并高亮显示的实例代码
2019/04/23 jQuery
JavaScript实现拖拽和缩放效果
2020/08/24 Javascript
js实现拖拽与碰撞检测
2020/09/18 Javascript
[45:15]Optic vs VP 2018国际邀请赛淘汰赛BO3 第一场 8.24
2018/08/25 DOTA
对于Python异常处理慎用“except:pass”建议
2015/04/02 Python
Python函数的周期性执行实现方法
2016/08/13 Python
Python实现的密码强度检测器示例
2017/08/23 Python
python实现从本地摄像头和网络摄像头截取图片功能
2019/07/11 Python
Python高级特性——详解多维数组切片(Slice)
2019/11/26 Python
opencv python Canny边缘提取实现过程解析
2020/02/03 Python
Tensorflow 实现将图像与标签数据转化为tfRecord文件
2020/02/17 Python
python根据完整路径获得盘名/路径名/文件名/文件扩展名的方法
2020/04/22 Python
使用Tensorflow-GPU禁用GPU设置(CPU与GPU速度对比)
2020/06/30 Python
美国折扣宠物药房:Total Pet Supply
2018/05/27 全球购物
教师自我评价范例
2013/09/24 职场文书
成功经营餐厅的创业计划书范文
2013/12/26 职场文书
丧事主持词大全
2014/04/02 职场文书
企业总经理助理岗位职责
2014/09/12 职场文书
PHP遍历数组的6种方式总结
2021/11/17 PHP
苹果电脑mac os中货币符号快捷输入
2022/02/17 杂记
Vertica集成Apache Hudi重磅使用指南
2022/03/31 Servers