当前位置:多学网学习教育电脑学习编程入门PHP教程PHP读取Access数据库操作类

PHP读取Access数据库操作类

[08-23 22:09:55]   来源:http://www.duoxue8.com  PHP教程   阅读:661
PHP读取Access数据库操作类,标签:PHP技巧,php培训,php学习,php安装,http://www.duoxue8.com

我们都知道PHP和MySQL是绝配,但有时候根据项目要求需要使用PHP读取Access数据库。其实PHP可以读取目前主流的所有数据库,因此读取Access数据库也不是很难。我整理了一个PHP读取Access数据库的类,提供给大家。

php 代码复制内容到剪贴板
  1. <?php   
  2. //Summary: Access数据库操作类   
  3. //使用范例:   
  4. //$database ="**.mdb";   
  5. //$dbuser    ="**";   
  6. //$dbpwd     ="**";   
  7. //$tablepre ="discuz_";   
  8. //$access = new Access($database,$dbuser,$dbpwd,$tablepre);   
  9. //$query = "SELECT * FROM ----"   
  10. //$access -> set_query($query);   
  11. //$access -> query();   
  12. class Access{   
  13.     var $databasepath;   
  14.     var $constr;   
  15.     var $dbusername;   
  16.     var $dbpassword;   
  17.     var $linkid;   
  18.     var $result;   
  19.     var $queryString;   
  20.     var $tablepre; //数据表前缀   
  21.     
  22.     function __construct($database,$dbuser,$dbpwd,$pre,$mode){   
  23.         $this->databasepath=$database;   
  24.         $this->username=$dbuser;   
  25.         $this->password=$dbpwd;   
  26.         $this->tablepre=$pre;   
  27.         $this->connect($mode);   
  28.     }   
  29.     
  30.     function Access($database,$dbuser,$dbpwd,$pre,$mode){   
  31.         $this->__construct($database,$dbuser,$dbpwd,$pre,$mode);   
  32.     }   
  33.     function connect($mode=false){   
  34.         $this->constr = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" . realpath($this->databasepath);   
  35.         if($mode){   
  36.             $this->linkid = odbc_pconnect($this->constr,$this->username,$this->password,SQL_CUR_USE_ODBC);   
  37.         } else {   
  38.             $this->linkid = odbc_connect($this->constr,$this->username, $this->password,SQL_CUR_USE_ODBC);   
  39.         }   
  40.         return $this->linkid;   
  41.     }   
  42.     function set_query($sql) {   
  43.         $prefix="#@__";   
  44.         $sql = trim($sql);   
  45.         $inQuote = false;   
  46.         $escaped = false;   
  47.         $quoteChar = '';   
  48.         $n = strlen($sql);   
  49.         $np = strlen($prefix);   
  50.         $restr = '';   
  51.         for($j=0; $j < $n; $j++) {   
  52.             $c = $sql{$j};   
  53.             $test = substr($sql, $j, $np);   
  54.             if(!$inQuote) {   
  55.                 if ($c == '"' || $c == "'") {  
  56.                     $inQuote = true;  
  57.                     $escaped = false;  
  58.                     $quoteChar = $c;  
  59.                 }  
  60.             } else {  
  61.                 if ($c == $quoteChar && !$escaped) {  
  62.                     $inQuote = false;  
  63.                 } else if ($c == "\" && !$escaped) {  
  64.                     $escaped = true;  
  65.                 } else {  
  66.                     $escaped = false;  
  67.                 }  
  68.             }  
  69.             if ($test == $prefix && !$inQuote) {  
  70.                 $restr .= $this->tablepre;  
  71.                 $j += $np-1;  
  72.             } else {  
  73.                 $restr .= $c;  
  74.             }  
  75.         }  
  76.         $this->queryString = $restr;  
  77.     }  
  78.     function query($id="me"){  
  79.         $this->result[$id] = @odbc_exec($this->linkid,$this->queryString);  
  80.         if(!$this->result[$id]) {  
  81.             $this->display_error("Execute Query False! <font color='red'>".$this->queryString."</font>");  
  82.         }  
  83.     }  
  84.     function first_array($id="me"){  
  85.          return odbc_fetch_array($this->result[$id]);  
  86.      }  
  87.      function fetch_row($id="me"){  
  88.          return odbc_fetch_row($this->result[$id]);  
  89.      }  
  90.      function total_num($id="me"){//取得记录总数  
  91.         return odbc_num_rows($this->result[$id]);  
  92.      }  
  93.      function close(){//关闭数据库连接函数  
  94.         @odbc_close($this->linkid);  
  95.         $this->free_all_result();  
  96.      }  
  97.      function free_result($id="me") {  
  98.          @mysql_free_result($this->result[$id]);  
  99.      }  
  100.      function free_all_result() {  
  101.          if(!is_array($this->result)) {  
  102.              return "";  
  103.          }  
  104.          foreach($this->result as $kk => $vv){  
  105.              if($vv) @odbc_free_result($vv);  
  106.          }  
  107.      }  
  108.      function insert($table,$field,$value){//插入记录函数  
  109.         $sql="INSERT INTO {$table} ({$field}) VALUES ({$value})";  
  110.         $this->set_query($sql);  
  111.         $this->query();  
  112.      }  
  113.      function getinfo($table,$requirement,$limit){//取得当条记录详细信息  
  114.         $sql="SELECT * FROM {$table} WHERE {$requirement}";  
  115.         $this->set_query($sql);  
  116.         $this->query();  
  117.          if($this->fetch_row()){  
  118.              for ($i=1;$i<$limit;$i++){  
  119.                 $info[$i]=odbc_result($this->result["me"],$i);  
  120.              }  
  121.          }  
  122.          return $info;  
  123.      }  
  124.      function getlist($table,$field,$limit,$condition,$sort="ORDER BY id DESC"){//取得记录列表  
  125.         $sql="SELECT * FROM ".$table." ".$condition." ".$sort;  
  126.         $this->set_query($sql);  
  127.         $this->query();  
  128.          while ($this->fetch_row()){  
  129.             $id=odbc_result($this->result["me"],1);  
  130.             $requirement="{$field}={$id}";  
  131.             $recordlist[]=getinfo($table,$requirement,$limit);  
  132.          }  
  133.          return $recordlist;  
  134.      }  
  135.      function getfieldlist($table,$field,$fieldnum,$condition="",$sort=""){//取得记录列表  
  136.         $sql="SELECT ".$field." FROM ".$table." ".$condition." ".$sort;  
  137.         $this->set_query($sql);  
  138.         $this->query();  
  139.          while ($this->fetch_row()){  
  140.              for ($j=0;$j<$fieldnum;$j++){  
  141.                 $info[$j]=odbc_result($this->result["me"],$j+1);  
  142.              }      
  143.             $rdlist[]=$info;  
  144.          }  
  145.          return $rdlist;  
  146.      }  
  147.      function updateinfo($table,$requirement,$set){//更新记录  
  148.         $sql="UPDATE {$table} SET {$set} WHERE {$requirement}";  
  149.         $this->set_query($sql);  
  150.         $this->query();  
  151.      }  
  152.      function deleteinfo($table,$requirement){//删除记录  
  153.         $sql="DELETE FROM {$table} WHERE {$requirement}";  
  154.         $this->set_query($sql);  
  155.         $this->query();  
  156.      }  
  157.      function deleterecord($table,$condition){//删除指定条件的记录  
  158.         $sql="DELETE FROM ".$table." WHERE ".$condition;  
  159.         $this->set_query($sql);  
  160.         $this->query();  
  161.      }  
  162.      function getcondrecord($table,$condition=""){// 取得指定条件的记录数  
  163.         $sql="SELECT COUNT(*) AS num FROM ".$table." ".$condition;  
  164.         $this->set_query($sql);  
  165.         $this->query();  
  166.         $num=odbc_num_rows($this->result["me"]);  
  167.          return $num;              
  168.      }  
  169.      function display_error($msg) {  
  170.          echo "<html>rn";  
  171.          echo "<head>rn";  
  172.          echo "<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>rn";  
  173.          echo "<title>DedeCms Error Track</title>rn";  
  174.          echo "</head>rn";  
  175.          echo "<body>rn<p style='line-helght:150%;font-size:10pt'>rn";  
  176.          echo $msg;  
  177.          echo "<br/><br/>";  
  178.          echo "</p>rn</body>rn";  
  179.          echo "</html>";   
  180.      }   
  181. }   
  182. ?>    

本文地址:http://www.frostsky.com/2011/04/php-access/
 




PHP读取Access数据库操作类 结束。
Tag:PHP教程PHP技巧,php培训,php学习,php安装电脑学习 - 编程入门 - PHP教程
PHP读取Access数据库操作类相关文章