php实现的pdo公共类定义与用法示例
本文实例讲述了php实现的pdo公共类定义与用法。分享给大家供大家参考,具体如下:
db.class.php:
dsn='mysql:host='.$dbHost.';dbname='.$dbName; $this->dbh=new\PDO($this->dsn,$dbUser,$dbPasswd); $this->dbh->setAttribute(\PDO::ATTR_EMULATE_PREPARES,false); $this->dbh->setAttribute(\PDO::ATTR_ERRMODE,\PDO::ERRMODE_EXCEPTION); $this->dbh->exec('SETcharacter_set_connection='.$dbCharset.';SETcharacter_set_client='.$dbCharset.';SETcharacter_set_results='.$dbCharset); }catch(Exception$e){ $this->outputError($e->getMessage()); } } publicstaticfunctiongetInstance($dbHost,$dbUser,$dbPasswd,$dbName,$dbCharset='utf8'){ if(self::$_instance===null){ self::$_instance=newself($dbHost,$dbUser,$dbPasswd,$dbName,$dbCharset); } returnself::$_instance; } publicfunctionfetchAll($sql,$params=array()){ try{ $stm=$this->dbh->prepare($sql); if($stm&&$stm->execute($params)){ return$stm->fetchAll(\PDO::FETCH_ASSOC); } }catch(Exception$e){ $this->outputError($e->getMessage()); } } publicfunctionfetchOne($sql,$params=array()){ try{ $result=false; $stm=$this->dbh->prepare($sql); if($stm&&$stm->execute($params)){ $result=$stm->fetch(\PDO::FETCH_ASSOC); } return$result; }catch(Exception$e){ $this->outputError($e->getMessage()); } } publicfunctionfetchColumn($sql,$params=array()){ $result=''; try{ $stm=$this->dbh->prepare($sql); if($stm&&$stm->execute($params)){ $result=$stm->fetchColumn(); } return$result; }catch(Exception$e){ $this->outputError($e->getMessage()); } } publicfunctioninsert($table,$params=array(),$returnLastId=true){ $_implode_field=''; $fields=array_keys($params); $_implode_field=implode(',',$fields); $_implode_value=''; foreach($fieldsas$value){ $_implode_value.=':'.$value.','; } $_implode_value=trim($_implode_value,','); $sql='INSERTINTO'.$table.'('.$_implode_field.')VALUES('.$_implode_value.')'; try{ $stm=$this->dbh->prepare($sql); $result=$stm->execute($params); if($returnLastId){ $result=$this->dbh->lastInsertId(); } return$result; }catch(Exception$e){ $this->outputError($e->getMessage()); } } publicfunctionupdate($table,$params=array(),$where=null){ $_implode_field=''; $_implode_field_arr=array(); if(empty($where)){ returnfalse; } $fields=array_keys($params); foreach($fieldsas$key){ $_implode_field_arr[]=$key.'='.':'.$key; } $_implode_field=implode(',',$_implode_field_arr); $sql='UPDATE'.$table.'SET'.$_implode_field.'WHERE'.$where; try{ $stm=$this->dbh->prepare($sql); $result=$stm->execute($params); return$result; }catch(Exception$e){ $this->outputError($e->getMessage()); } } publicfunctiondelete($sql,$params=array()){ try{ $stm=$this->dbh->prepare($sql); $result=$stm->execute($params); return$result; }catch(Exception$e){ $this->outputError($e->getMessage()); } } publicfunctionexec($sql,$params=array()){ try{ $stm=$this->dbh->prepare($sql); $result=$stm->execute($params); return$result; }catch(Exception$e){ $this->outputError($e->getMessage()); } } privatefunctionoutputError($strErrMsg){ thrownewException("MySQLError:".$strErrMsg); } publicfunction__destruct(){ $this->dbh=null; } }
实例:
12,); $res=$pdo->fetchOne($sql,$parame); var_dump($res); $sql='SELECT*FROMcms_link'; $result=$db->fetchAll($sql); print_r($result); //查询记录数量 $sql='SELECTCOUNT(*)FROMcms_link'; $count=$db->fetchColumn($sql); echo$count; $data=array( 'siteid'=>1, 'linktype'=>1, 'name'=>'google', 'url'=>'http://www.google.com', 'listorder'=>0, 'elite'=>0, 'passed'=>1, 'addtime'=>time() ); $lastInsertId=$db->insert('cms_link',$data); echo$lastInsertId; //用try try{ $result=$pdo->insert('news',$essay); }catch(Exception$e){ error_log($e->getMessage()); error_log($e->getMessage().'in'.__FILE__.'online'.__LINE__); saveLog('url文章:'.$essay['link'].'数据插入失败
'); continue; } $data=array( 'siteid'=>1, 'linktype'=>1, 'name'=>'google', 'url'=>'http://www.google.com', 'listorder'=>0, 'elite'=>0, 'passed'=>1, 'addtime'=>time() ); $db->insert('cms_link',$data); $sql='DELETEFROMcms_linkWHERElinkid=4'; $result=$db->delete($sql); var_dump($result);
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP基于pdo操作数据库技巧总结》、《php+Oracle数据库程序设计技巧总结》、《PHP+MongoDB数据库操作技巧大全》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。