PHP操作Postgresql类方法教程

[复制链接]
查看1466 | 回复0 | 2020-11-21 17:59 | 显示全部楼层 |阅读模式

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.       
  6.     if(!$PG)  
  7.     {  
  8.         $db_error = "无法连接到PostGreSQL数据库!";  
  9.         echo $db_error;   
  10.     }  
  11.     else  
  12.     {  
  13.         echo "成功连接!";  
  14.         $query = "select name from ex where gid = 2";  
  15.         $result = $PG->query($query);  
  16.         $row = $PG->fetchRow();  
  17.         echo $row[0];  
  18.     }  
  19. ?>
复制代码

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

UID
1
贡献
387
丢币
38902
主题
4607
回帖
116
注册时间
2018-9-25
最后登录
2024-4-16