connect(); } /** * 连接数据库 * * @access protected */ protected function connect(){ if(!class_exists('pdo')) $this->halt('不支持pdo扩展!'); try{ $this->_pdo = new pdo( 'mysql:dbname='.$this->options["dbname"].';host='.$this->options["host"].'; port='.$this->options['port'], $this->options["user"], $this->options["password"], array(pdo::mysql_attr_init_command => "set names utf8") ); $this->_pdo->setattribute(pdo::attr_errmode, pdo::errmode_exception); $this->_pdo->setattribute(pdo::attr_emulate_prepares, false); $this->_connected = true; }catch (pdoexception $e){ $this->halt('不能链接mysql数据库 .error:'.$e->getmessage()); } } /** * 初始化. * * @param stirng $sql * @param array|string $parameters * [url=home.php?mod=space&uid=987628]@return[/url] void */ private function _init($sql ,$parameters = ""){ if(!$this->_connected) $this->connect(); try { $this->_query = $this->_pdo->prepare($sql); $this->bindmore($parameters); if(!empty($this->_parameters)) { foreach($this->_parameters as $key=>$value){ switch( true ) { case is_int($value): $type = pdo::param_int; break; case is_bool($value): $type = pdo::param_bool; break; case is_null($value): $type = pdo::param_null; break; default: $type = pdo::param_str; } $this->_query->bindparam($key ,$this->_parameters[$key] ,$type); } } $this->_parameters = array(); return $this->_query->execute(); }catch(pdoexception $e){ $this->halt($e->getmessage() ,$this->_query->querystring); } } /** * 绑定数据 * * @param string $para * @param string $value */ public function bind($para, $value){ $this->_parameters[":" . $para] = $value; } /** * 绑定数据 * * @param array $parray */ public function bindmore($parray){ if(empty($this->_parameters) && is_array($parray)) { foreach($parray as $key => &$value) { $this->bind($key, $parray[$key]); } } } /** * 一些简单的操作 * * @param string $sql * @param array $params * @param int $mode * [url=home.php?mod=space&uid=987628]@return[/url] mixed */ public function query($sql,$params = null, $mode = pdo::fetch_assoc){ $sql = trim($sql); if($params){ $this->_init($sql ,$params); return $this->_query->fetchall($mode); }else{ $sql = str_replace('`waf_' ,'`'.$this->options['prefix'] ,$sql); return $this->_pdo->exec($sql); } return false; } /** * 根据查询取出所有资料 * * @param string $sql * @param mixed $params * @return array */ public function fetchall($sql ,array $params = null){ $this->_init($sql ,$params); $result = array(); while($row = $this->_query->fetch(pdo::fetch_assoc)){ $result[] = $row; } return $result; } /** * 查询单条数据 * * @param string $sql * @param mixed $params * @return array */ public function fetchone($sql ,array $params = null){ $this->_init($sql ,$params); $result = $this->_query->fetch(pdo::fetch_assoc); return $result; } /** * 获取总数量 * * @param string $sql * @param array $params * @return int */ public function getcount($sql ,$params = null){ $this->_init($sql ,$params); $result = $this->_query->fetch(pdo::fetch_num); return $result[0]; } /** * 函数返回结果集中行的数目 * * @param string $sql * @return int */ public function numrows($sql) { $this->_filter = array(); $datas = $this->fetchall($sql); $counts = count($datas); unset($datas); return $counts; } /** * 函数返回结果集中字段的数 * * @param string $sql * @return int */ public function numfields($sql) { $this->_filter = array(); $this->_init($sql); return $this->_query->columncount() ; } /** * 获取最后插入的id * * @return mixed */ public function insertid() { try { return $this->_pdo->lastinsertid(); }catch( pdoexception $e ) { $this->halt($e->getmessage() ,$this->_query->querystring); return false; } } /** * 关闭连接. * * @access public */ public function close(){ $this->_pdo = null; } /** * 接. * * @access public */ public function __destruct() { $this->close(); } /** * 写入数据 * * @param string $table * @param array $data * @return int */ public function insert($table ,$data){ try { $sql = "insert into ".table($table); $columns = $values = ''; foreach( $data as $k=>$v ) { $columns .= "$k,"; $values .= ":$k,"; } $columns = trim($columns,','); $values = trim($values,','); $sql = $sql . " ($columns) values ($values)"; $this->_init($sql ,$data); unset($sql ,$columns ,$values); return $this->insertid(); }catch( pdoexception $e ) { $this->halt($e->getmessage() ,$this->_query->querystring); return false; } } /** * 更新数据 * * @param string $table * @param array $data * @param mixed $wheres * @return int */ public function update($table ,$data ,$wheres = null){ $sql = "update ".table($table)." set "; foreach($data as $key=>$value){ if(is_array($value)){ $sql .= "`{$key}` = ".$key . $value[0] . $value[1] . ','; unset($data[$key]); }else{ $sql .= "`{$key}`=:{$key},"; } } $sql = trim($sql ,','); if(is_array($wheres) && !empty($wheres)){ $where = ''; foreach($wheres as $key=>$value){ $where .= "`{$key}`=:{$key} and "; } $where = trim($where ,' and '); $sql .= " where {$where}"; $data = array_merge($data ,$wheres); unset($where ,$wheres); }else{ $sql .= " where {$wheres}"; } $this->_init($sql ,$data); unset($sql); return $this->_query->rowcount(); } /** * 删除数据 * * @param string $table * @param array|string $wheres * @return int */ public function delete($table ,$wheres = null){ $sql = "delete from ".table($table).' where '; if(is_array($wheres)){ foreach($wheres as $key => $value){ $sql .= "`{$key}`=:{$key} and "; } $sql = trim($sql ,' and '); }else{ $sql .= $wheres; } $this->_init($sql ,$wheres); unset($sql); return $this->_query->rowcount(); } }
以上就是mysqpdo封装的内容。
