[原]超级简单访问PostgreSQL的小型php类
Posted by bianbian on 2007-03-17 07:55
本文Tags: php, PostgreSQL, 数据库, 类
实在受不了ADOdb之类的庞大库了,还有网上下的几个库也超级难用,我自己写了一个。只当简单的用,事务之类的我还没有用到,所以没有写进去。感兴趣的可以补充。好像里面的函数有些要php 4.2以上的。
使用示例:
- <?php
- $db = & new PostgreSQL();
- //single record:
- $record = & $db->query_first("SELECT * FROM table WHERE id=xxx");
- //many lines:
- if ($records = & $db->query_all("SELECT * FROM table WHERE id>xxx")) {
- foreach ($records as &$record) {
- ...
- }
- }
- $db->close();
- ?>
类的代码:
- <?php
- /* PostgreSQL 8.* Simple Usage Class
- * copyleft (c) bianbian
- * http://bianbian.org
- */
- class PostgreSQL {
- var $dbhost = 'localhost';
- var $dbuser = 'postgres';
- var $dbpw = '';
- var $dbname = '';
- var $dbport = 5432;
- var $pconnect = 1;
- var $querynum = 0;
- var $dbconnect;
- function PostgreSQL($pconnect=1) {
- $this->pconnect = $pconnect;
- $this->connect();
- }
- function connect() {
- $connect = "host=".$this->dbhost." port=".$this->dbport." dbname=".$this->dbname." user=".$this->dbuser;
- if (!empty($this->dbpw))
- $connect.=" password=".$this->dbpw;
- if ($this->pconnect)
- $this->dbconnect=pg_pconnect($connect);
- else
- $this->dbconnect=pg_connect($connect);
- if (!$this->dbconnect)
- $this->halt("Cannot connect to database");
- }
- function query($sql, $silence = 0) {
- $result = pg_query($this->dbconnect, $sql);
- if(!$result && !$silence) {
- $this->halt('PgSQL Query Error', $sql);
- }
- $this->querynum++;
- return $result;
- }
- // does a query and returns first row by reference
- function & query_first($sql, $result_type = PGSQL_ASSOC) {
- $result = $this->query($sql);
- $returnarray=pg_fetch_array($result, 0, $result_type);
- $this->free_result($result);
- return $returnarray;
- }
- function & query_all($sql, $result_type = PGSQL_ASSOC) {
- $result = $this->query($sql);
- $returnarray = (function_exists("pg_fetch_all"))
- ? pg_fetch_all($result)
- : $this->fetch_all($result, $result_type);
- $this->free_result($result);
- return $returnarray;
- }
- function fetch_array($result, $row, $result_type = PGSQL_ASSOC) {
- return pg_fetch_array($result, $row, $result_type);
- }
- function fetch_all($result) {
- while ($row = pg_fetch_assoc($result)) {
- $array_out[] = $row;
- }
- return $array_out;
- }
- function affected_rows() {
- return pg_affected_rows();
- }
- function error() {
- return pg_last_error();
- }
- function num_rows(&$query) {
- return pg_num_rows($query);
- }
- function num_fields(&$query) {
- return pg_num_rows($query);
- }
- function free_result(&$query) {
- return pg_free_result($query);
- }
- function fetch_row(&$query) {
- return pg_fetch_row($query);
- }
- function close() {
- if (!$this->pconnect)
- return pg_close();
- }
- function halt($message = '', $sql = '') {
- $timestamp = time();
- $errmsg = '';
- $dberror = $this->error();
- if($message) {
- $errmsg = "<b>info</b>: $message\n\n";
- }
- $errmsg .= "<b>Time</b>: ".gmdate("Y-n-j g:ia", $timestamp)."\n";
- $errmsg .= "<b>Script</b>: ".$_SERVER['PHP_SELF']."\n\n";
- $errmsg = nl2br($errmsg);
- if($sql) {
- $errmsg .= "<!--\n\nSQL: $sql\n";
- }
- $errmsg .= "Error: $dberror\n\n-->";
- echo "<p>$errmsg</p>";
- exit;
- }
- }
- ?>
遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道
April 13th, 2007 at 10:15:45
谢谢了!很好用!