• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP操作Postgresql封装类步骤详解

php 搞代码 3年前 (2022-01-22) 18次浏览 已收录 0个评论

这次给大家带来PHP操作Postgresql封装类步骤详解,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; // 已执行的查询总数/* 类构造函数,用来初始化$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->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_ar<strong style="color:transparent">本文来源gao@daima#com搞(%代@#码@网&</strong><strong>搞gaodaima代码</strong>ray($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];  }?>

相信看了本文案例你已经掌握了方法,更多精彩请关注搞代码其它相关文章!

推荐阅读:

php7操作MongoDB增删改查步骤详解

PHP单例模式使用案例详解

以上就是PHP操作Postgresql封装类步骤详解的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP操作Postgresql封装类步骤详解
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址