当前位置: 首页 > 后端技术 > PHP

一个非常实用的php数据库pdo操作类(curd操作类)

时间:2023-03-30 05:15:19 PHP

操作类文件db.class.phppdo=newPDO("mysql:host=".$config['db_host'].";port=".$config['db_port'].";dbname=".$config['db_name'],$config['db_user'],$config['db_pass']);}catch(PDOException$e){//无法链接数据库,初次使用请先配置数据库!退出($e->getMessage());}$this->prefix=$config['db_prefix'];$this->pdo->exec("设置名称UTF8");}//配置表信息publicfunctionset_table($table=null,$primary='id'){if($table==null){exit('NotfoundTable');}$this->primary=$primary;$this->table=$this->prefix.$table;返回$这个;}//获取数据publicfunctiongetData($sql){if(!$result=$this->query($sql))returnarray();if(!$this->Statement->rowCount())returnarray();$行=数组();while($rows[]=$this->Statement->fetch(PDO::FETCH_ASSOC)){}$this->Statement=null;array_pop($行);返回$行;}//查询数据条数publicfunctiongetCount($conditions){$where='';if(is_array($conditions)){$join=array();foreach($conditionsas$key=>$value){$value='\''.$value.'\'';$join[]="{$key}={$value}";}$where="WHERE".join("AND",$join);}else{if(null!=$conditions)$where="WHERE".$conditions;}$sql="SELECTcount(*)asFrcountFROM{$this->table}{$where}";$result=$this->getData($sql);return$result[0]['Frcount'];}//获取单字节内容publicfunctiongetField($where=null,$fields=null){if($record=$this->findAll($where,null,$fields,1)){$res=array_pop($record);return$res[$fields];}else{returnFALSE;}}//递增数据publicfunctiongoInc($conditions,$field,$vp=1){$where="";if(is_array($conditions)){$join=array();foreach($conditionsas$key=>$value){$value='\''.$value.'\'';$join[]="{$key}={$value}";}$where="WHERE".join("AND",$join);}else{if(null!=$conditions)$where="WHERE".$conditions;}$values="{$field}={$field}+{$vp}";$sql="UPDATE{$this->table}SET{$values}{$where}";return$this->pdo->;执行($sql);}//递减publicfunctiongoDec($conditions,$field,$vp=1){return$this->goInc($conditions,$field,-$vp);}//修改数据publicfunctionupdate($conditions,$row){$where="";$row=$this->__prepera_format($row);如果(空($row)){返回FALSE;}if(is_array($conditions)){$join=array();foreach($conditionsas$key=>$condition){$condition='\''.$condition.'\'';$join[]="{$key}={$condition}";}$where="WHERE".join("AND",$join);}else{if(null!=$conditions){$where="WHERE".$conditions;}}foreach($rowas$key=>$value){$value='\''.$value.'\'';$vals[]="{$key}={$value}";}$values=join(",",$vals);$sql="更新{$this->table}设置{$values}{$where}";//echo$sql.'
';$res=$this->pdo->exec($sql);if($res){return$res;}else{var_dump($this->pdo->errorInfo());}}//查询所有publicfunctionfindAll($conditions=null,$order=null,$fields=null,$limit=null){$where='';if(is_array($conditions)){$join=array();foreach($conditionsas$key=>$value){$value='\''.$value.'\'';$join[]="{$key}={$value}";}$where="WHERE".join("AND",$join);}else{if(null!=$conditions)$where="WHERE".$conditions;}if(is_array($order)){$where.='ORDERBY';$where.=implode(',',$order);}else{if($order!=null)$where.="ORDERBY".$order;}if(!empty($limit))$where.="LIMIT{$limit}";$fields=empty($字段)?“*”:$字段;$sql="SELECT{$fields}FROM{$this->table}{$where}";返回$this->getData($sql);}//查询公共函数find($where=null,$order=null,$fields=null,$limit=1){if($record=$this->findAll($where,$order,$fields,1)){返回array_pop($record);}else{返回假;}}//执行SQL语句并检查错误publicfunctionquery($sql){$this->filter[]=$sql;$this->Statement=$this->pdo->query($sql);if($this->Statement){返回$this;}else{$msg=$this->pdo->errorInfo();if($msg[2])exit('数据库错误:'.$msg[2].end($this->filter));}}//执行SQL语句functionpublicfunctionfindSql($sql){return$this->getData($sql);}//根据条件(conditions)删除publicfunctiondelete($conditions){$where="";如果我s_array($conditions)){$join=array();foreach($conditionsas$key=>$condition){$condition='\''.$condition.'\'';$join[]="{$key}={$condition}";}$where="WHERE(".join("AND",$join).")";}else{if(null!=$conditions)$where="WHERE(".$conditions.")";}$sql="从{$this->table}{$where}中删除";返回$this->pdo->exec($sql);}//新增数据publicfunctionadd($row){if(!is_array($row)){returnFALSE;}$row=$this->__prepera_format($row);如果(空($row)){返回FALSE;}foreach($rowas$key=>$value){$cols[]=$key;$vals[]='\''.$value.'\'';}$col=join(',',$cols);$val=join(',',$vals);$sql="插入{$this->table}({$col})VALUES({$val})";如果(假!=$this->pdo->exec($sql)){if($newinserid=$this->pdo->lastInsertId()){返回$newinserid;}else{$a=$this->find($row,"{$this->primary}DESC",$this->primary);返回array_pop($a);}}返回假;}privatefunction__prepera_format($rows){$stmt=$this->pdo->prepare('DESC'.$this->table);$stmt->执行();$columns=$stmt->fetchAll(PDO::FETCH_COLUMN);$newcol=数组();foreach($columnsas$col){$newcol[$col]=null;}返回array_intersect_key($rows,$newcol);}}instantiatedclass'localhost',//数据库地址'db_port'=>3306,//默认mysql数据库端口'db_name'=>'test',//数据库名'db_user'=>'root',//数据库用户名称'数据库_pass'=>'root',//数据库密码'db_prefix'=>'',//表前缀];//实例化类$db=newDB_API($config);//表名$article=$db->set_table('article');//新数据$newdata=['title'=>'这是一个标题'];$r=$article->add($newdata);if($r){echo'添加成功!';}else{echo'操作失败!';}//查询数据$where=['id'=>3];$find=$article->find($where);//查询一条数据$find=$article->findAll($where);//查询多条数据print_r($find);//更新数据$where=['title'=>'helloworld666'];$update=$article->update(['id'=>1],$where);if($update){echo'更新成功!';//查询并打印$newdata=$article->find('id=1');print_r($newdata);}else{echo'更新失败!';}//删除数据$where=['id'=>1];$del=$article->delete($where);if($del){echo'删除成功!';}else{echo'删除失败!';}//获取符合条件的记录数$where=['author'=>'TANKING'];$count=$article->getCount($where);回声$计数;//执行原生SQL语句$sql='select*fromarticlewhereid=3';$lists=$article->findSql($sql);print_r($lists);//根据条件查询对应字段的值$where=['id'=>1];$res=$article->getField($where,'title');if($res){echo$res;}else{echo"nodata";}//高级查询//$conditions查询条件//$order排序方式//$fields指定字段//$limit查询个数$res=$article->findAll($conditions=null,$order='idasc',$fields=null,$limit=null);if($res){print_r($res);}else{print_r("Nodata");}作者:TANKING