如何实现PHP操作Postgresql封装类及完整应用实例?
- 内容介绍
- 文章标签
- 相关推荐
本文共计828个文字,预计阅读时间需要4分钟。
本示例讲述了PHP操作PostgreSQL封装类与应用。分享给广大用户提供参考,具体如下:
这个类封装了一些常用函数,并包含事务处理的内容,方便后续学习。
类文件定义:php
public function __construct($host, $port, $dbname, $user, $password) { $this->host=$host; $this->port=$port; $this->dbname=$dbname; $this->user=$user; $this->password=$password; $this->connect(); }
private function connect() { $this->conn=pg_connect(host=$this->host port=$this->port dbname=$this->dbname user=$this->user password=$this->password); if (!$this->conn) { die(数据库连接失败: . pg_last_error()); } }
public function query($sql) { $result=pg_query($this->conn, $sql); if (!$result) { die(查询失败: . pg_last_error()); } return $result; }
public function beginTransaction() { pg_query($this->conn, BEGIN); }
public function commit() { pg_query($this->conn, COMMIT); }
public function rollback() { pg_query($this->conn, ROLLBACK); }
public function __destruct() { pg_close($this->conn); }}?>
使用示例:php
$db=new PostgreSQLDB($host, $port, $dbname, $user, $password);
$db->beginTransaction();$db->query(INSERT INTO your_table (column1, column2) VALUES ('value1', 'value2'));$db->commit();?>
本文实例讲述了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基于pdo操作数据库技巧总结》、《php+Oracle数据库程序设计技巧总结》、《PHP+MongoDB数据库操作技巧大全》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
本文共计828个文字,预计阅读时间需要4分钟。
本示例讲述了PHP操作PostgreSQL封装类与应用。分享给广大用户提供参考,具体如下:
这个类封装了一些常用函数,并包含事务处理的内容,方便后续学习。
类文件定义:php
public function __construct($host, $port, $dbname, $user, $password) { $this->host=$host; $this->port=$port; $this->dbname=$dbname; $this->user=$user; $this->password=$password; $this->connect(); }
private function connect() { $this->conn=pg_connect(host=$this->host port=$this->port dbname=$this->dbname user=$this->user password=$this->password); if (!$this->conn) { die(数据库连接失败: . pg_last_error()); } }
public function query($sql) { $result=pg_query($this->conn, $sql); if (!$result) { die(查询失败: . pg_last_error()); } return $result; }
public function beginTransaction() { pg_query($this->conn, BEGIN); }
public function commit() { pg_query($this->conn, COMMIT); }
public function rollback() { pg_query($this->conn, ROLLBACK); }
public function __destruct() { pg_close($this->conn); }}?>
使用示例:php
$db=new PostgreSQLDB($host, $port, $dbname, $user, $password);
$db->beginTransaction();$db->query(INSERT INTO your_table (column1, column2) VALUES ('value1', 'value2'));$db->commit();?>
本文实例讲述了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基于pdo操作数据库技巧总结》、《php+Oracle数据库程序设计技巧总结》、《PHP+MongoDB数据库操作技巧大全》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。

