PHP operation Postgresql encapsulation class and complete application example

不言
Release: 2023-03-24 18:54:02
Original
1686 people have browsed it

This article mainly introduces the PHP operation Postgresql encapsulation class, and analyzes the common connection, query, statistics and other operation encapsulation techniques and usage methods of PHP for Postgresql database in the form of examples. Friends in need can refer to the following

The examples in this article describe the PHP operation of Postgresql encapsulation classes and applications. I share it with you for your reference. The details are as follows:

This class encapsulates some commonly used functions. The original post also contains transaction processing content. Let’s learn more about it later.

Class file definition:


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; } } ?>
Copy after login


The tested php was released together, and another php in the LAN was also tested. With a postgresql server, I feel that the query speed is very fast, and querying postgregis data is also very good.


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]; } ?>
Copy after login

Related recommendations:

postgreSQL php and client


The above is the detailed content of PHP operation Postgresql encapsulation class and complete application example. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!