您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 辽源分类信息网,免费分类信息发布

Mysql PDO基础操作

2024/4/25 7:21:44发布10次查看
tablekey = $tablekey; } /** * 获取库文件信息 * */ public function getconfig() { if( ! isset($this->tablekey) ) { throw new qexception('当前操作表不存在'); } $dbconfig = $this->dbconfig(); require($dbconfig); $this->configfile = $tbl[$this->tablekey]; if( !isset( $this->configfile )) { throw new qexception('当前操作表配置不存在'.$this->tablekey); } } /** * 数据库链接 * */ public function init() { $this->getconfig(); if( ! file_exists($this->configfile['configfile']) ) { throw new qexception('当前链接配置不存在'.$this->configfile['configfile']); } require $this->configfile['configfile']; try { $this->pdo = new pdo( $dsn, $user, $password, array(pdo::mysql_attr_init_command => "set names ".$encode,pdo::fetch_assoc=>true)); //$this->pdo->setattribute(pdo::attr_errmode,pdo::errmode_warning); //php预处理错误 } catch (exception $e) { exit('数据库连接失败,错误信息:'. $e->getmessage()); } } /** * 表组合 * */ private function tableprefix() { $this->getconfig(); return $this->configfile['tableprefix'].$this->tablekey; } /** * sql执行 * * @param string $sql */ private function query( $sql ) { $this->init(); return $this->pdo->query($sql); } /** * 反回执行受影响行数 * * 适合于 插入 、修改 * @param string $sql */ private function exec( $sql ) { $this->init(); return $this->pdo->exec($sql); } /** * 获取一条数据 * * @access public * @param string $tablekey 数据表标识 * @param array $rule 数据查询规则 * @return array */ public function findone( $param ) { $param = $this->combined( $param ); $where = !empty($param['where']) ? ' where '.$param['where'] : ' '.$param['limit'] ; $sql = 'select '.$param['col'].' from `'.$this->tableprefix().'`'.$where.' limit 1'; return $this->query($sql)->fetch(); } /** * 获取多条数据 * * @access public * @param string $tablekey 数据表标识 * @param array $rule 数据查询规则 * @return array */ public function find( $param ) { $param = $this->combined( $param ); $where = !empty($param['in']) ? ' where '.$param['in'] : ' '.$param['limit'] ; $sql = 'select '.$param['col'].' from `'.$this->tableprefix().'`'.$where; return $this->query($sql)->fetchall(); } /** * 获取多条数据(数据分页时用) * * @access public * @param string $tablekey 数据表标识 * @param array $rule 数据查询规则 * @return array */ public function findall($param) { } /** * 统计数据 * * @access public * @param string $tablekey 数据表标识 * @param array $rule 数据查询规则 * @return int */ public function count($param) { $param = $this->combined( $param ); $sql = 'select count('.$param['count'].') as total from `'.$this->tableprefix().'` where '.$param['where']; return $this->query($sql)->fetch(); } /** * 创建一条数据 * * @access public * @param array $param 数据创建规则 * @return int 0失败、大于0成功 */ public function create($param) { $sql = 'insert into `'.$this->tableprefix().'` '.$this->deal($param).';'; return $this->exec($sql) ? $this->pdo->lastinsertid() : false; } /** * 修改数据 * * @access public * @param string $tablekey 数据表标识 * @param array $data 数据信息[键值对] * @param array $rule 数据修改规则 * @return bool */ public function modify($param) { $param = $this->combined( $param ); $sql = 'update `'.$this->tableprefix().'` set '.$param['set'].' where '.$param['where'].';'; return $this->exec($sql) ? $this->pdo->lastinsertid() : false; } /** * 删除数据 * * @access public * @param array $param 数据删除规则 * @return bool */ public function remove($param) { $param = $this->combined( $param ); $where = !empty($param['in']) ? $param['in'] : $param['where'] ; $sql = 'delete from `'.$this->tableprefix().'` where '.$where; return $this->exec($sql) ? $this->pdo->lastinsertid() : false; } /** * 处理(查询)数组数据 * * @param array $param */ private function combined( $param ) { $r = array( 'col' => null, 'set' => null, 'where' => null, 'in' => null, 'group' => null, 'order' => null, 'limit' => null, 'count' => null, 'pagerows' => null, 'point' => null, ); //处理查询字段 if(isset($param['col']) && !empty($param['col'])) { $r['col'] = $param['col']; } //处理修改字段 if(isset($param['set']) && !empty($param['set'])) { $r['set'] .= $this->where( $param['set'] ); } //处理where条件字段 if(isset($param['where']) && !empty($param['where'])) { $r['where'] .= $this->where( $param['where'] ); } //处理in条件字段(尽量少用) if(isset($param['in']) && !empty($param['in'])) { $str = $keys = null; foreach ($param['in'] as $key=>$val) { $keys .= $key; } if(empty($keys)) {return false;} foreach ($param['in'][$keys] as $val) { $str .= $val.","; } $r['in'] = $keys.' in ('.trim($str ,',').')'; } //处理group条件字段 if(isset($param['group']) && !empty($param['group'])) { $r['group'] .= 'group by '.$param['group']; } //处理order条件字段 if(isset($param['order']) && !empty($param['order'])) { $desc = isset($param['desc']) && !empty($param['desc']) ? $param['desc'] : 'asc'; $r['order'] .= 'order by '.$param['order'].' '.$desc; } //处理limit条件字段 if(isset($param['limit']) && !empty($param['limit'])) { $limit = isset($param['limit']) && !empty($param['limit']) ? $param['limit'] : 1; $r['limit'] .= 'limit '.$limit; } //处理limit条件字段 if(isset($param['count']) && !empty($param['count'])) { $limit = isset($param['count']) && !empty($param['count']) ? $param['count'] : '*'; $r['count'] .= $limit; } //处理分页显示条件 if(isset($param['pagerows']) && !empty($param['pagerows'])) { $r['pagerows'] .= $param['pagerows']; } //分页数 if(isset($param['point']) && !empty($param['point'])) { $r['point'] .= $param['point']; } //同时判断 if(isset($param['symbol']) && !empty($param['symbol'])) { $str = $param['symbol']; $bol = !empty($str['than']) ? $str['link'].' '.$str['than'] : ''; $r['symbol'] .= $str['larger'].' '.$bol; } return $r; } /** * 处理(where)组合语句 * * @param array $param */ private function where ( $param ) { $str = null; foreach ($param as $key =>$val) { if(is_int($val)) { $str .= "`{$key}` = {$val} "; } else { $str .= "`{$key}` = '{$val}' "; } } return $str; } /** * 处理(插入)数组数据 * */ private function deal( $param ) { $keys = $value = $vals = null; foreach( $param as $key => $val ) { $keys .= "`".$key."`,"; $vals .= "'".$val."',"; } $keys = trim($keys , ','); $vals = trim($vals , ','); $value = "({$keys}) value ({$vals})"; return $value; } /** * 析构函数 */ public function __destruct() { unset($this->pdo); unset($this->configfile); unset($this->tablekey); }
辽源分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录