仿ZF功能的Mysql数据库操作类
<?php /** * Mysql数据库操作类 v1.0 * Aboc QQ:9986584 * */ class DbMysql { /** * 在数据库操作中,只对数据库操作有影响的字符做转义 * 当此类正常后,所有数据操作 @ */ /* * 数据库连接句柄 */ private $_Db = NULL; /* * 是否持续连接 0.1 */ private $_pconnect = 0; /* * 编码 */ private $_charset = 'gbk'; /* * 默认数据库配置 */ private $_config = array ('dbhost' => 'localhost', 'username' => 'root', 'password' => 'root', 'dbname' => 'test' ); /** * 初始连接数据库 */ function __construct($config) { if (empty($config)) $config = array(); $this->checkConfig ( $config ); $this->connect (); $this->query ( 'set names ' . $this->_charset ); //设置编码 } /** * 判断config变量 * * @param unknown_type $config */ private function checkConfig($config) { foreach ( $config as $key => $value ) { $this->_config [$key] = empty ( $value ) ? $this->_config [$key] : $value; } //return $this->_config; } /* * 连接数据库 */ private function connect() { if ($this->_pconnect) { $this->_Db = mysql_pconnect ( $this->_config ['dbhost'], $this->_config ['username'], $this->_config ['password'] ) or die ( '数据库连接失败' . mysql_errno () ); } else { $this->_Db = mysql_pconnect ( $this->_config ['dbhost'], $this->_config ['username'], $this->_config ['password'] ) or die ( '数据库连接失败' . mysql_errno () ); } if ($this->_Db != NULL) { mysql_select_db ( $this->_config ['dbname'], $this->_Db ) or die ( '数据库' . $this->_config ['dbname'] . '不存在' ); } } /** * 将变量的单引号或双引号转义 * * @param unknown_type $string */ private function strtag($string) { if (is_array ( $string )) { foreach ( $string as $key => $value ) { $stringnew [$this->strtag ( $key )] = $this->strtag ( $value ); } } else { //在此做转义,对单引号 //TODO 好像 %也要转义吧? $stringnew = mysql_real_escape_string ( $string ); //$stringnew=str_replace(array("'",'"'),array('*','*'),$stringnew); } return $stringnew; } /** * 将数组转化为SQL接受的条件样式 * * @param unknown_type $array */ private function chageArray($array) { //MYSQL支持insert into joincart set session_id = 'dddd',product_id='44',number='7',jointime='456465' //所以更新和插入可以使用同一组数据 $array = $this->strtag ( $array ); //转义 $str = ''; foreach ( $array as $key => $value ) { $str .= empty ( $str ) ? '`' . $key . '`="' . $value . '"' : ', `' . $key . '`="' . $value . '" '; } return $str; } /** * 执行查询语句 * @return bool */ public function query($sql) { echo $sql.'<br>'; if (! $result = mysql_query ( $sql, $this->_Db)) { die ( '数据库查询失败' . mysql_error () ); } else { return $result; } } public function select() { } /** * 插入记录 * */ public function insert($table, $array) { $sql = 'insert into `' . $table . '` set ' . $this->chageArray ( $array ); if ($this->query ( $sql )) { return $this->lastId (); } else { return false; } } /** * 更新记录 * */ public function update($table, $array, $where = NULL) { if ($where == NULL) { $sql = 'update `' . $table . '` set ' . $this->chageArray ( $array ); } else { $sql = 'update `' . $table . '` set ' . $this->chageArray ( $array ) . ' where ' . $where; } if ($this->query ( $sql )) { return true; } else { return false; } } /** * 删除记录 * */ public function delete($table, $where = NULL) { if ($where == NULL) { $sql = 'delete from `' . $table . '`'; } else { $sql = 'delete from `' . $table . '` where ' . $where; } if ($this->query ( $sql )) { return true; } else { return false; } } /** * 获取一条记录 * */ public function fetchRow($sql) { $reult = $this->query ( $sql ); $row = mysql_fetch_assoc ( $reult ); return $row; } /** * 获取所有记录 * */ public function fetchAll($sql) { $result = $this->query ( $sql ); if ($result !== false) { $arr = array (); while ( $row = mysql_fetch_assoc ( $result ) ) { $arr [] = $row; } return $arr; } else { return false; } return $result; } /** * 获取最后一次影响的Id * */ public function lastId() { return mysql_insert_id ( $this->_Db ); } /** * 获取符合条件的记录数 * */ public function fetchNum($sql) { $reult = $this->query ( $sql ); $num = mysql_num_rows ( $reult ); return $num; } /** * 输出适合的where语句 */ public function quoteInto($string,$value ) { $value = $this->strtag($value); $string = str_replace('?',"'". $value ."'",$string); return $string; } /** * 释放查询结果 */ public function free() { mysql_free_result($this->_Db); } /** * */ function __destruct() { } } ?>
以上用到的数据库操作类我稍后会放出来