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

(编辑:jimmy 日期: 2024/10/3 浏览:2)

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

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

类文件定义:

<"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;
}
}
"htmlcode">
<"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];
  }
"_blank" href="https://www.jb51.net/Special/192.htm">PHP基于pdo操作数据库技巧总结》、《php+Oracle数据库程序设计技巧总结》、《PHP+MongoDB数据库操作技巧大全》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

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

一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。