霜天部落 | 专注PHP研发,研究LAMP高性能架构部署与优化

PHP读取Access数据库操作类

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

 set_query($query);
//$access -> query();
class Access{
	var $databasepath;
	var $constr;
	var $dbusername;
	var $dbpassword;
	var $linkid;
	var $result;
	var $queryString;
	var $tablepre; //数据表前缀
	
	function __construct($database,$dbuser,$dbpwd,$pre,$mode){
		$this->databasepath=$database;
		$this->username=$dbuser;
		$this->password=$dbpwd;
		$this->tablepre=$pre;
		$this->connect($mode);
	}
	
	function Access($database,$dbuser,$dbpwd,$pre,$mode){
		$this->__construct($database,$dbuser,$dbpwd,$pre,$mode);
	}
	function connect($mode=false){
		$this->constr = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" . realpath($this->databasepath);
		if($mode){
			$this->linkid = odbc_pconnect($this->constr,$this->username,$this->password,SQL_CUR_USE_ODBC);
		} else {
			$this->linkid = odbc_connect($this->constr,$this->username, $this->password,SQL_CUR_USE_ODBC);
		}
		return $this->linkid;
	}
	function set_query($sql) {
		$prefix="#@__";
		$sql = trim($sql);
		$inQuote = false;
		$escaped = false;
		$quoteChar = '';
		$n = strlen($sql);
		$np = strlen($prefix);
		$restr = '';
		for($j=0; $j < $n; $j++) {
			$c = $sql{$j};
			$test = substr($sql, $j, $np);
			if(!$inQuote) {
				if ($c == '"' || $c == "'") {
					$inQuote = true;
					$escaped = false;
					$quoteChar = $c;
				}
			} else {
				if ($c == $quoteChar && !$escaped) {
					$inQuote = false;
				} else if ($c == "\\" && !$escaped) {
					$escaped = true;
				} else {
					$escaped = false;
				}
			}
			if ($test == $prefix && !$inQuote) {
				$restr .= $this->tablepre;
				$j += $np-1;
			} else {
				$restr .= $c;
			}
		}
		$this->queryString = $restr;
	}
	function query($id="me"){
		$this->result[$id] = @odbc_exec($this->linkid,$this->queryString);
		if(!$this->result[$id]) {
			$this->display_error("Execute Query False! ".$this->queryString."");
		}
	}
	function first_array($id="me"){
         return odbc_fetch_array($this->result[$id]);
     }
     function fetch_row($id="me"){
         return odbc_fetch_row($this->result[$id]);
     }
     function total_num($id="me"){//取得记录总数
        return odbc_num_rows($this->result[$id]);
     }
     function close(){//关闭数据库连接函数
        @odbc_close($this->linkid);
        $this->free_all_result();
     }
     function free_result($id="me") {
         @mysql_free_result($this->result[$id]);
     }
     function free_all_result() {
         if(!is_array($this->result)) {
             return "";
         }
         foreach($this->result as $kk => $vv){
             if($vv) @odbc_free_result($vv);
         }
     }
     function insert($table,$field,$value){//插入记录函数
        $sql="INSERT INTO {$table} ({$field}) VALUES ({$value})";
        $this->set_query($sql);
        $this->query();
     }
     function getinfo($table,$requirement,$limit){//取得当条记录详细信息
        $sql="SELECT * FROM {$table} WHERE {$requirement}";
        $this->set_query($sql);
        $this->query();
         if($this->fetch_row()){
             for ($i=1;$i<$limit;$i++){
                $info[$i]=odbc_result($this->result["me"],$i);
             }
         }
         return $info;
     }
     function getlist($table,$field,$limit,$condition,$sort="ORDER BY id DESC"){//取得记录列表
        $sql="SELECT * FROM ".$table." ".$condition." ".$sort;
        $this->set_query($sql);
        $this->query();
         while ($this->fetch_row()){
            $id=odbc_result($this->result["me"],1);
            $requirement="{$field}={$id}";
            $recordlist[]=getinfo($table,$requirement,$limit);
         }
         return $recordlist;
     }
     function getfieldlist($table,$field,$fieldnum,$condition="",$sort=""){//取得记录列表
        $sql="SELECT ".$field." FROM ".$table." ".$condition." ".$sort;
        $this->set_query($sql);
        $this->query();
         while ($this->fetch_row()){
             for ($j=0;$j<$fieldnum;$j++){
                $info[$j]=odbc_result($this->result["me"],$j+1);
             }    
            $rdlist[]=$info;
         }
         return $rdlist;
     }
     function updateinfo($table,$requirement,$set){//更新记录
        $sql="UPDATE {$table} SET {$set} WHERE {$requirement}";
        $this->set_query($sql);
        $this->query();
     }
     function deleteinfo($table,$requirement){//删除记录
        $sql="DELETE FROM {$table} WHERE {$requirement}";
        $this->set_query($sql);
        $this->query();
     }
     function deleterecord($table,$condition){//删除指定条件的记录
        $sql="DELETE FROM ".$table." WHERE ".$condition;
        $this->set_query($sql);
        $this->query();
     }
     function getcondrecord($table,$condition=""){// 取得指定条件的记录数
        $sql="SELECT COUNT(*) AS num FROM ".$table." ".$condition;
        $this->set_query($sql);
        $this->query();
        $num=odbc_num_rows($this->result["me"]);
         return $num;            
     }
     function display_error($msg) {
         echo "\r\n";
         echo "\r\n";
         echo "\r\n";
         echo "\r\n";
         echo "\r\n";
         echo "\r\n

\r\n"; echo $msg; echo "

"; echo "

\r\n\r\n"; echo ""; } } ?>