Microsoft Access+php,增加,删除,读取,修改class

5 篇文章 0 订阅
1 篇文章 0 订阅

1、windows server
2、Microsoft Access+php,增加,删除,读取,修改class

<?php
class db_access {
	var $querynum = 0;
	var $conn;
	var $insertid = 0;
	var $cursor = 0;

	function connect($dbhost, $dbuser = '', $dbpw = '', $dbname = '', $pconnect = 0) {
		$this->conn = new com('adodb.connection') or exit('Cannot start ADO');
		$this->conn->open("DRIVER={Microsoft Access Driver (*.mdb)};dbq=$dbhost;uid=$dbuser;pwd=$dbpw");
		if($this->conn->state == 0) {
			$this->conn->open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$dbhost");
			if($this->conn->state == 0)	exit('Can not connect to Access Database !');
		}
		define('NUM', 1);
		define('ASSOC', 2);
		define('BOTH', 3);
	}

	function query($sql, $type = '', $expires = 3600, $dbname = '') {
		$this->querynum++;
		$sql = trim($sql);
		if(preg_match("/^(select.*)limit ([0-9]+)(,([0-9]+))?$/i", $sql, $matchs)) {
			$sql = $matchs[1];
			$offset = $matchs[2];
			$pagesize = $matchs[4];
			$query = $this->conn->Execute($sql);
			return $this->limit($query, $offset, $pagesize);
		} else {
			return $this->conn->Execute($sql);
		}
	}

	function get_one($query) {
		$this->querynum++;
	    $rs = $this->conn->Execute($query);
		$r = $this->fetch_array($rs);
		$this->free_result($rs);
		return $r;
	}

	function fetch_array($rs, $result_type = 3) {
		if(is_array($rs)) {
			return $this->cursor < count($rs) ? $rs[$this->cursor++] : FALSE;
		} else {
			if($rs->EOF) return FALSE;
			$array = array();
			for($i = 0; $i < $this->num_fields($rs); $i++) {
				$fielddata = $rs->Fields[$i]->Value;
			    if($result_type == NUM || $result_type == BOTH) $array[$i] = $fielddata;
			    if($result_type == ASSOC || $result_type == BOTH) $array[$rs->Fields[$i]->Name] = $fielddata;
			}
			$rs->MoveNext();
			return $array;
		}
	}

	function affected_rows($rs) {
		return count($rs);
	}

	function num_rows($rs) {
	    return is_array($rs) ? count($rs) : $rs->recordcount;
	}

	function num_fields($rs) {
	    return $rs->Fields->Count;
	}

	function fetch_assoc($rs) {
	    return $this->fetch_array($rs, ASSOC);
	}

	function fetch_row($rs) {
	    return $this->fetch_array($rs, NUM);
	}

	function free_result($rs) {
	    if(is_resource($rs)) $rs->close();
	}

	function error() {
	    return $this->conn->Errors[$this->conn->Errors->Count-1]->Number;
	}

	function errormsg() {
	    return $this->conn->Errors[$this->conn->Errors->Count-1]->Description;
	}

	function close() {
	    $this->conn->close();
	}

	function limit($rs, $offset, $pagesize = 0) {
		if($pagesize > 0) {
			$rs->Move($offset);
		} else {
			$pagesize = $offset;
		}
		$info = array();
		for($i = 0; $i < $pagesize; $i++) {
			$r = $this->fetch_array($rs);
			if(!$r) break;
			$info[] = $r;
		}
		$this->free_result($rs);
		$this->cursor = 0;
		return $info;
	}
}
?>
  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

net1193379199

您的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值