当前位置:首页 > PHP教程 > php类库 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2021-09-12 15:02:17 浏览: 评论:0 

这篇文章主要介绍了PHP操作Postgresql封装类,结合实例形式分析了php针对Postgresql数据库常见的连接、查询、统计等操作封装技巧与使用方法,需要的朋友可以参考下

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

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

类文件定义:

  1. <?php 
  2. class pgsql { 
  3. private $linkid; // PostgreSQL连接标识符 
  4. private $host; // PostgreSQL服务器主机 
  5. private $port; // PostgreSQL服务器主机端口 
  6. private $user; // PostgreSQL用户 
  7. private $passwd; // PostgreSQL密码 
  8. private $db; // Postgresql数据库 
  9. private $result; // 查询的结果 
  10. private $querycount; // 已执行的查询总数 
  11. /* 类构造函数,用来初始化$host、$user、$passwd和$db字段。 */ 
  12. function __construct($host, $port ,$db, $user, $passwd) { 
  13. $this->host = $host; 
  14. $this->port = $port; 
  15. $this->user = $user; 
  16. $this->passwd = $passwd; 
  17. $this->db = $db; 
  18. } 
  19. /* 连接Postgresql数据库 */ 
  20. function connect(){ 
  21. try{ 
  22. $this->linkid = @pg_connect("host=$this->host port=$this->port dbname=$this->db 
  23. user=$this->user password=$this->passwd"); 
  24. if (! $this->linkid) 
  25. throw new Exception("Could not connect to PostgreSQL server."); 
  26. } 
  27. catch (Exception $e) { 
  28. die($e->getMessage()); 
  29. } 
  30. } 
  31. /* 执行数据库查询。 */ 
  32. function query($query){ 
  33. try{ 
  34. $this->result = @pg_query($this->linkid,$query); 
  35. if(! $this->result) 
  36. throw new Exception("The database query failed."); 
  37. } 
  38. catch (Exception $e){ 
  39. echo $e->getMessage(); 
  40. } 
  41. $this->querycount++; 
  42. return $this->result; 
  43. } 
  44. /* 确定受查询所影响的行的总计。 */ 
  45. function affectedRows(){ 
  46. $count = @pg_affected_rows($this->linkid); 
  47. return $count; 
  48. } 
  49. /* 确定查询返回的行的总计。 */ 
  50. function numRows(){ 
  51. $count = @pg_num_rows($this->result); 
  52. return $count; 
  53. } 
  54. /* 将查询的结果行作为一个对象返回。 */ 
  55. function fetchObject(){ 
  56. $row = @pg_fetch_object($this->result); 
  57. return $row; 
  58. } 
  59. /* 将查询的结果行作为一个索引数组返回。 */ 
  60. function fetchRow(){ 
  61. $row = @pg_fetch_row($this->result); 
  62. return $row; 
  63. } 
  64. /* 将查询的结果行作为一个关联数组返回。 */ 
  65. function fetchArray(){ 
  66. $row = @pg_fetch_array($this->result); 
  67. return $row; 
  68. } 
  69. /* 返回在这个对象的生存期内执行的查询总数。这不是必须的,但是您也许会感兴趣。 */ 
  70. function numQueries(){ 
  71. return $this->querycount; 
  72. } 
  73. } 
  74. ?> 

测试的php一并放出,另外测试了下局域网内的另一台postgresql服务器,感觉查询速度还是很快的,查询postgregis数据也是杠杠滴。

  1. <?php 
  2.   include 'PGDB.php'; 
  3.   $PG = new pgsql("192.168.1.167", "5432", "postgis", "postgres", "post"); 
  4.   $PG->connect(); 
  5.   if(!$PG) 
  6.   { 
  7.     $db_error = "无法连接到PostGreSQL数据库!"; 
  8.     echo $db_error; 
  9.   } 
  10.   else 
  11.   { 
  12.     echo "成功连接!"; 
  13.     $query = "select name from ex where gid = 2"; 
  14.     $result = $PG->query($query); 
  15.     $row = $PG->fetchRow(); 
  16.     echo $row[0]; 
  17.   } 
  18. ?> 

 

Tags: Postgresql PHP封装类

分享到:

相关文章