bianbian coding life

便便代码人生: 关注技术, 翻译文档, 偶尔动动手

[原]超级简单访问PostgreSQL的小型php类

Posted by bianbian on 2007-03-17 07:55

本文Tags: , , ,

实在受不了ADOdb之类的庞大库了,还有网上下的几个库也超级难用,我自己写了一个。只当简单的用,事务之类的我还没有用到,所以没有写进去。感兴趣的可以补充。好像里面的函数有些要php 4.2以上的。
使用示例:

  1. <?php
  2. $db = & new PostgreSQL();
  3. //single record:
  4. $record = & $db->query_first("SELECT * FROM table WHERE id=xxx");
  5. //many lines:
  6. if ($records = & $db->query_all("SELECT * FROM table WHERE id>xxx")) {
  7.     foreach ($records as &$record) {
  8.         ...
  9.     }
  10. }
  11. $db->close();
  12. ?>

类的代码:

  1. <?php
  2. /* PostgreSQL 8.* Simple Usage Class
  3. * copyleft (c) bianbian
  4. * http://bianbian.org
  5. */
  6.  
  7. class PostgreSQL {
  8.  
  9.     var $dbhost   = 'localhost';
  10.     var $dbuser   = 'postgres';
  11.     var $dbpw     = '';
  12.     var $dbname   = '';
  13.     var $dbport   = 5432;
  14.     var $pconnect = 1;
  15.  
  16.     var $querynum = 0;
  17.     var $dbconnect;
  18.  
  19.     function PostgreSQL($pconnect=1) {
  20.         $this->pconnect = $pconnect;
  21.         $this->connect();
  22.     }
  23.    
  24.     function connect() {
  25.         $connect = "host=".$this->dbhost." port=".$this->dbport." dbname=".$this->dbname." user=".$this->dbuser;
  26.         if (!empty($this->dbpw))
  27.             $connect.=" password=".$this->dbpw;
  28.        
  29.         if ($this->pconnect)
  30.             $this->dbconnect=pg_pconnect($connect);
  31.         else
  32.             $this->dbconnect=pg_connect($connect);
  33.  
  34.         if (!$this->dbconnect)
  35.             $this->halt("Cannot connect to database");
  36.     }
  37.  
  38.     function query($sql, $silence = 0) {
  39.         $result = pg_query($this->dbconnect, $sql);
  40.         if(!$result && !$silence) {
  41.             $this->halt('PgSQL Query Error', $sql);
  42.         }
  43.         $this->querynum++;
  44.         return $result;
  45.     }
  46.  
  47.     // does a query and returns first row by reference
  48.     function & query_first($sql, $result_type = PGSQL_ASSOC) {
  49.         $result = $this->query($sql);
  50.         $returnarray=pg_fetch_array($result, 0, $result_type);
  51.         $this->free_result($result);
  52.         return $returnarray;
  53.     }
  54.    
  55.     function & query_all($sql, $result_type = PGSQL_ASSOC) {
  56.         $result = $this->query($sql);
  57.         $returnarray = (function_exists("pg_fetch_all"))
  58.             ? pg_fetch_all($result)
  59.             : $this->fetch_all($result, $result_type);
  60.         $this->free_result($result);
  61.         return $returnarray;
  62.     }
  63.    
  64.     function fetch_array($result, $row, $result_type = PGSQL_ASSOC) {
  65.         return pg_fetch_array($result, $row, $result_type);
  66.     }
  67.    
  68.     function fetch_all($result) {
  69.         while ($row = pg_fetch_assoc($result)) {
  70.             $array_out[] = $row;
  71.         }
  72.         return $array_out;
  73.     }
  74.    
  75.     function affected_rows() {
  76.         return pg_affected_rows();
  77.     }
  78.  
  79.     function error() {
  80.         return pg_last_error();
  81.     }
  82.  
  83.     function num_rows(&$query) {
  84.         return pg_num_rows($query);
  85.     }
  86.  
  87.     function num_fields(&$query) {
  88.         return pg_num_rows($query);
  89.     }
  90.  
  91.     function free_result(&$query) {
  92.         return pg_free_result($query);
  93.     }
  94.  
  95.     function fetch_row(&$query) {
  96.         return pg_fetch_row($query);
  97.     }
  98.  
  99.     function close() {
  100.         if (!$this->pconnect)
  101.             return pg_close();
  102.     }
  103.  
  104.     function halt($message = '', $sql = '') {
  105.         $timestamp = time();
  106.         $errmsg = '';
  107.    
  108.         $dberror = $this->error();
  109.    
  110.         if($message) {
  111.             $errmsg = "<b>info</b>: $message\n\n";
  112.         }
  113.         $errmsg .= "<b>Time</b>: ".gmdate("Y-n-j g:ia", $timestamp)."\n";
  114.         $errmsg .= "<b>Script</b>: ".$_SERVER['PHP_SELF']."\n\n";
  115.         $errmsg = nl2br($errmsg);
  116.         if($sql) {
  117.             $errmsg .= "<!--\n\nSQL: $sql\n";
  118.         }
  119.         $errmsg .= "Error:  $dberror\n\n-->";
  120.    
  121.         echo "<p>$errmsg</p>";
  122.         exit;
  123.     }
  124. }
  125. ?>
标签: , , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

One Response to “[原]超级简单访问PostgreSQL的小型php类”

  1. zhuanzhuan Says:

    谢谢了!很好用!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

(required)