<? /* @author VOID(空) @date 2006-12-21 @desc 数据访问简单包装 @oicq 78252859 @email lkf5_303@163.com @mobile 13183857698 */ class DataAccWrap { var $dbObj = null; //数据库访问对象 var $isLog = true; //是否记录日志 var $logFile = "dataAcc.txt"; //日志文件名 var $dataTable = null; //操作的数据表 function DataAcc($dbObj) { $this->dbObj = $dbObj; } //+----------------------------------------------------------------------------------------------------------- //Desc:添加数据操作(只限单表操作) function save($hashTable) { if(!is_array($hashTable) && $this->isLog) $this->writeLog("参数类型错误!"); if($this->isLog && $this->dataTable == null) $this->writeLog('没有指定数据表','Y'); $field = ""; $value = ""; foreach($hashTable as $k => $v) { $v = preg_replace( "/'/", "\\'", $v ); $field .= "$k,"; $value .= "'$v',"; } $field = preg_replace("/,$/","" ,$field); $value = preg_replace("/,$/","" ,$value); $sql = "insert into $this->dataTable($field) values($value)"; return $this->execute($sql); } //+----------------------------------------------------------------------------------------------------------- //Desc:更新数据操作(只限单表操作) function update($hashTable,$updateCase ='') { if(!is_array($hashTable) && $this->isLog) $this->writeLog("参数类型错误!"); if($this->isLog && $this->dataTable == null) $this->writeLog('没有指定数据表','Y'); $updateFields = ""; $loop = 1; $len = count($hashTable); foreach($hashTable as $k => $v) { if(!empty($v)) { $v = preg_replace( "/'/", "\\'", $v ); if($loop == $len) { $updateFields .= $k . "='".$v."'"; } else { $updateFields .= $k . "='".$v."',"; } $loop++; } } if($updateCase == '') { $sql = "update $this->dataTable set $updateFields"; } else { $sql = "update $this->dataTable set $updateFields where $updateCase"; } return $this->execute($sql); } //+----------------------------------------------------------------------------------------------------------- //Desc:删除数据操作(只限单表操作) function delete($delCase = '') { if($this->isLog && $this->dataTable == null) $this->writeLog('没有指定数据表','Y'); if($delCase =='') { $sql = "delete from $this->dataTable"; } else { $sql = "delete from $this->dataTable where $delCase"; } return $this->execute($sql); } //+----------------------------------------------------------------------------------------------------------- //Desc:简单数据查询操作(只限单表操作) function select($selectCase = '') { if($this->isLog && $this->dataTable == null) $this->writeLog('没有指定数据表','Y'); if($selectCase == '') { $sql = "select * from $this->dataTable"; } else { $sql = "select * from $this->dataTable $selectCase"; } return $this->execute($sql); } //+----------------------------------------------------------------------------------------------------------- //Desc:发送SQL指令并执行操作 function execute($sql) { if($this->isLog) $this->writeLog('执行SQL语句: '.$sql); $this->dbObj->connect(); $result = $this->dbObj->query($sql); $this->dbObj->close(); return $result; } //+----------------------------------------------------------------------------------------------------------- //Desc:日志记录,便于调试 function writeLog($logTxt,$stop ='N') { $fp = fopen($this->logFile,"a"); $dateStr = date("Y-m-d h:i:s",time()); $logTxt = "[$dateStr] $logTxt\r\n"; fputs($fp,$logTxt); fclose($fp); if($stop == 'Y') die('发生致命错误,停止处理'); } } ?> |