PHP + ADO + ACCESS 类库

不是我想用ACCESS的,么办法啊,用户即上帝么。
更何况这上帝不懂数据库啥的……与其劝他换个服务商还不如委屈自己写个类库将就一下。
网上有ODBC版本,但在我这用有点小小的版本问题……又是让人无奈的上帝。

贴代码啦,代码晕眩症的就不用点全文链接了。

  1. <?php
  2. /*--------------------------------------------------------------------
  3. FileName:db.php
  4. Summary: PHP-Access Database Operation Class
  5. Author:  Jerry Hong
  6. CreateTime: 2007-8-10    
  7. LastModifed: 2007-8-25 : Added UTF-8 Support
  8. copyright (c)2007
  9. http://www.JerryHong.com 
  10. [email]hjrgenius@163.com[/email]
  11. --------------------------------------------------------------------*/
  12. class ADODB
  13. {
  14.     var $conn;
  15.     var $rs;        //RecordSet
  16.     var $fld;        //Field
  17.     var $results;    //Mixed array for query result.
  18.    
  19.     function ADODB($dbname, $dbuser='', $dbpassword='') {
  20.         return $this->__construct($dbname, $dbuser, $dbpassword);
  21.     }
  22.  
  23.     /**
  24.      * Connects to a database
  25.      * @param string $dbname
  26.      * @param string $dbuser
  27.      * @param string $dbpassword
  28.      */
  29.     function __construct($dbname, $dbuser, $dbpassword) {
  30.         $this->conn = new COM("ADODB.Connection", NULL, 65001) or bail('Cannot create ado connection, please contact administrator'.$adminEmail);
  31.         register_shutdown_function(array(&$this, "__destruct"));
  32.         $connStr="DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath($dbname).
  33.                                                             ";UID=".$dbuser.
  34.                                                             ";PWD=".$dbpassword;
  35.         //echo $connStr;
  36.         $this->conn->Open($connStr);
  37.         $this->conn->charPage ='65001';
  38.         /*
  39.         PHP5 can use these codes to replace the up line:
  40.         try{
  41.             $this->conn->Open($connStr) ;
  42.         }
  43.         catch(exception $e){
  44.             $this->bail('Cannot open database, please contact administrator');
  45.         }
  46.         */
  47.         return true;
  48.     }
  49.  
  50.     //GOT BUG HERE!
  51.     function __destruct() {
  52.         if($this->rs)
  53.         {
  54.             //$this->rs->Close();
  55.             //$this->rs->Release();
  56.         }
  57.         if($this->conn)
  58.         {
  59.             //$this->conn->Close();
  60.             //$this->conn->Release();
  61.         }
  62.         unset($this);
  63.         return true;   
  64.     }
  65.  
  66.     function __sleep(){
  67.  
  68.     }
  69.     //Data Funcs//////////////////////////////////////////////
  70.     /**
  71.      * Execute a query
  72.      * @param string $sql
  73.      */
  74.     function query($sql) {
  75.         $this->rs = $this->conn->Execute($sql);   
  76.     }
  77.  
  78.     /**
  79.      * Return an entire result set from the database
  80.      * @param string $query (can also be null to pull from the cache)
  81.      * @return mixed results
  82.      */
  83.     function get_results($sql) {
  84.         $results=array();
  85.         $this->query($sql);
  86.         $num_columns = $this->get_col_num();
  87.         for ($i=0; $i < $num_columns; $i++)
  88.         {
  89.             $this->fld[$i] =$this->rs->Fields($i);
  90.         }
  91.         $rowcount = 0;
  92.         while (!$this->rs->EOF)
  93.         {
  94.             for ($i=0; $i < $num_columns; $i++)
  95.             {
  96.                 $results[$rowcount][$this->fld[$i]->name] = $this->fld[$i]->value;
  97.             }
  98.             $rowcount++;            // rowcount 自增
  99.             $this->rs->MoveNext();
  100.         }
  101.         return $results;
  102.     }
  103.  
  104.     /**
  105.      * Return an row of result set from the database
  106.      * @param string $query (can also be null to pull from the cache)
  107.      * @return mixed results
  108.      */
  109.     function get_result($sql) {
  110.         $result=array();
  111.         $this->query($sql);
  112.         $num_columns = $this->get_col_num();
  113.         for ($i=0; $i < $num_columns; $i++)
  114.         {
  115.             $this->fld[$i] =$this->rs->Fields($i);
  116.         }
  117.         for ($i=0; $i < $num_columns; $i++)
  118.         {
  119.             $result[$this->fld[$i]->name] = $this->fld[$i]->value;
  120.         }
  121.         return $result;
  122.     }
  123.     /**
  124.      * Get the number of columns of one table. Should run after "query('select * from table');" or it won't know the table name!
  125.      * @return int column number
  126.      */
  127.     function get_col_num() {
  128.         $col_num= $this->rs->fields->Count();
  129.         return $col_num;
  130.     }
  131.  
  132.     //Tool Funcs////////////////////////////////
  133.     /**
  134.      * Starts the timer, for debugging purposes
  135.      */
  136.     function timer_start() {
  137.         $mtime = microtime();
  138.         $mtime = explode(' ', $mtime);
  139.         $this->time_start = $mtime[1] + $mtime[0];
  140.         return true;
  141.     }
  142.  
  143.     /**
  144.      * Stops the debugging timer
  145.      * @return int total time spent on the query, in milliseconds
  146.      */
  147.     function timer_stop() {
  148.         $mtime = microtime();
  149.         $mtime = explode(' ', $mtime);
  150.         $time_end = $mtime[1] + $mtime[0];
  151.         $time_total = $time_end - $this->time_start;
  152.         return $time_total;
  153.     }
  154.  
  155.     /**
  156.      * Wraps fatal errors in a nice header and footer and dies.
  157.      * @param string $message
  158.      */
  159.     function bail($message) { // Just wraps errors in a nice header and footer
  160.         header('Content-Type: text/html; charset=utf-8');
  161. ?>
  162.  
  163. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  164. <html xmlns="http://www.w3.org/1999/xhtml">
  165. <head>
  166.     <title>database &rsaquo; Error</title>
  167.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  168. </head>
  169. <body>
  170.     <p><?php echo $message; ?></p>
  171. </body>
  172. </html>
  173. <?php
  174.         die();
  175.     }
  176. }
  177.  
  178.  
  179. if ( ! isset($db) )
  180.     $db = new ADODB(DB_FILE,DB_USER,DB_PASSWORD);
  181. ?>

Related posts

2 Responses to “PHP + ADO + ACCESS 类库”


  1. 1 (沙发) 小伙伴

    谢谢!初学,正在找PHP和Access相关的资料,请问这个类该怎么使用呢?

  2. 2 (板凳) Jerry

    @小伙伴(- -||)
    1.在主程序中定义DB_FILE,DB_USER,DB_PASSWORD这三个值
    2.在主程序中require这个类库
    3.在主程序中使用$db->query($sql) 或 getresults($sql) 或 getresult($sql);(如果是在函数中别忘了先global $db;)
    后两个有返回值,一个是二维数组,一个是一维数组,可以用print_r()看看。

Leave a Reply