<?php //FileName:Access.class.php //Summary: Access数据库操作类 //Author: forest //CreateTime: 2006-8-10 //LastModifed: //copyright (c)2006 //http://freeweb.nyist.net/~chairy //[email]chaizuxue@163.com[/email] // 使用范例: //$databasepath="database.mdb"; //$dbusername=""; //$dbpassword=""; //include_once("Access.class.php"); //$access=new Access($databasepath,$dbusername,$dbpassword); // 2008.12.23 Aboc Edit /9986584 // 在判断字段是否为空时,用 is null / is not null // 2011.4.30 Aboc Edit class Access { var $databasepath, $constr, $dbusername, $dbpassword, $link; function Access($databasepath, $dbusername, $dbpassword) { $this->databasepath = $databasepath; $this->username = $dbusername; $this->password = $dbpassword; $this->connect (); } function connect() { $this->constr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath ( $this->databasepath ); $this->link = odbc_connect ( $this->constr, $this->username, $this->password, SQL_CUR_USE_ODBC ); if (!$this->link)die('数据库连接失败'); return $this->link; } function query($sql) { return @odbc_exec ( $this->link, $sql ); } function first_array($sql) { return odbc_fetch_array ( $this->query ( $sql ) ); } function fetch_row($query) { return odbc_fetch_row ( $query ); } /** * 取得记录总数 * * @param unknown_type $sql * @return unknown */ function total_num($sql) { return odbc_num_rows ( $this->query ( $sql ) ); } /** * 关闭连接 * */ function close() { odbc_close ( $this->link ); } /** * 插入 * * @param unknown_type $table * @param unknown_type $array */ function insert($table, $array) { if (empty($array))return false; $k = array(); $v = array(); foreach ($array as $key=>$value){ if (!is_numeric($value)){ $value = trim($value); $value = str_replace("'", "\'", $value); } $k[] = "`$key`"; $v[] = "'$value'"; } $sql = "INSERT INTO " . $table . " (" . join(',', $k) . ") VALUES (" . join(',', $v) . ")"; return $this->query ( $sql ); } /** * 获取详情 * Enter description here ... * @param unknown_type $table * @param unknown_type $field * @param unknown_type $id * @param unknown_type $colnum */ function getinfo($table, $field, $id, $colnum) { $sql = "SELECT * FROM " . $table . " WHERE " . $field . "=" . $id . ""; $query = $this->query ( $sql ); if ($this->fetch_row ( $query )) { for($i = 1; $i < $colnum; $i ++) { $info [$i] = odbc_result ( $query, $i ); } } return $info; } /** * 取得记录列表 * * @param unknown_type $table * @param unknown_type $field * @param unknown_type $colnum * @param unknown_type $condition * @param unknown_type $sort * @return unknown */ function getlist($table, $field, $colnum, $condition, $sort = "ORDER BY id DESC") { $sql = "SELECT * FROM " . $table . " " . $condition . " " . $sort; //echo $sql; $query = $this->query ( $sql ); $i = 0; while ( $this->fetch_row ( $query ) ) { $recordlist [$i] = $this->getinfo ( $table, $field, odbc_result ( $query, 1 ), $colnum ); $i ++; } return $recordlist; } /** * 取得记录列表 * * @param unknown_type $table * @param unknown_type $field * @param unknown_type $fieldnum * @param unknown_type $condition * @param unknown_type $sort * @return unknown */ function getfieldlist($table, $field, $fieldnum, $condition = "", $sort = "") { $sql = "SELECT " . $field . " FROM " . $table . " " . $condition . " " . $sort; $query = $this->query ( $sql ); $i = 0; $rdlist = array(); while ( $this->fetch_row ( $query ) ) { $info = array(); for($j = 0; $j < $fieldnum; $j ++) { $info [$j] = odbc_result ( $query, $j + 1 ); } $rdlist [$i] = $info; $i ++; } return $rdlist; } /** * 获取所有数据 * Enter description here ... * @param unknown_type $table * @param unknown_type $field * @param unknown_type $where * @param unknown_type $limit * @param unknown_type $order */ function fetchAll($table,$field=array(),$where,$limit='',$order=''){ $where = !empty($where)?"where $where":''; $limit = $limit!=''?"top $limit":''; $order = $order != ''?" order by $order":''; $sql = "select $limit ".join(',', $limit)." from $table $where $order"; $query = $this->query ( $sql ); $i = 0; $fieldnum = count($field); $rdlist = array(); while ( $this->fetch_row ( $query ) ) { $info = array(); for($j = 0; $j < $fieldnum; $j ++) { $info [$field[$j]] = odbc_result ( $query, $j + 1 ); } $rdlist [$i] = $info; $i ++; } return $rdlist; } /** * 更新 * * @param unknown_type $table * @param unknown_type $field * @param unknown_type $id * @param unknown_type $set */ function updateinfo($table, $field, $id, $set) { $where = ! empty ( $field ) ? " WHERE " . $field . "=" . $id : ""; $sql = "UPDATE " . $table . " SET " . $set . $where; return $this->query ( $sql ); } /** * 更新2 * Enter description here ... * @param unknown_type $table * @param unknown_type $array * @param unknown_type $where */ function update($table,$array,$where){ $where = !empty($where)?"where $where":''; if (empty($array))return false; $set = ''; foreach ($array as $key=>$value){ if (!is_numeric($value)){ $value = trim($value); $value = str_replace("'", "\'", $value); } $set .= !empty($set)?",`$key`='$value'":"`$key`='$value'"; } return $this->query ("update $table set $set $where "); } /** * 删除 * * @param unknown_type $table * @param unknown_type $field * @param unknown_type $id */ function deleteinfo($table, $field, $id) { $where = ! empty ( $field ) ? " WHERE " . $field . "=" . $id : ""; $sql = "DELETE FROM " . $table . $where; //echo $sql;exit; return $this->query ( $sql ); } /** * 删除2 * Enter description here ... * @param unknown_type $table * @param unknown_type $where */ function delete($table,$where){ $where = !empty($where)?"where $where":''; return $this->query ("DELETE FROM " . $table . $where); } /** * 依条件删除/带where * * @param unknown_type $table * @param unknown_type $condition */ function deleterecord($table, $condition) { $sql = "DELETE FROM " . $table . " WHERE " . $condition; $this->query ( $sql ); } /** * 取得指定条件的记录数/不带where * * @param unknown_type $table * @param unknown_type $condition * @return unknown */ function getcondrecord($table, $condition = "") { $sql = "SELECT COUNT(*) AS num FROM " . $table . " " . $condition; $query = $this->query ( $sql ); $this->fetch_row ( $query ); $num = odbc_result ( $query, 1 ); return $num; } } ?>
Access的PHP操作类[修改]
2012-1-30 17:29 Monday
分类: PHP 评论(74) 浏览(34650)
+1
26
前一篇: 修改session存放方式为Mysql 的类
后一篇:似QQ的右下角提示窗
留下你的看法: