用接口实现的一个pdo工具类
<?php
//定义一个pdo工具类
class DAOPdo implements I_Dao
{
//配置信息
private $_host;
private $_root;
private $_dbname;
private $_pw;
private $_port;
private $_charset;
//pdo对象
private $pdo;
//pdo结果集对象
private $result;
//被影响的记录数
private $affected_row;
//当前类的实例化对象
private static $instance;
private function __construct($option){
//初始化服务器配置信息
$this->_initArray($option);
//初始化pdo
$this->_initPdo();
}
//定义一个单例模式
public static function getSingleton(array $option=array()){
if(!(self::$instance instanceof self)){
self::$instance = new self ($option);
}
return self::$instance;
}
private function __clone(){
}
private function _initArray($option){
$this->_host=isset($option['host'])?$option['host']:'';
$this->_pw=isset($option['pw'])?$option['pw']:'';
$this->_root=isset($option['root'])?$option['root']:'';
$this->_dbname=isset($option['dbname'])?$option['dbname']:'';
$this->_port=isset($option['port'])?$option['port']:'';
$this->_charset=isset($option['charset'])?$option['charset']:'';
}
private function _initPdo(){
try{
$dns="mysql:host=$this->_host;dbname=$this->_dbname;port=$this->_port;charset=$this->_charset";
$this->pdo=new PDO($dns,$this->_root,$this->_pw);
}
//捕获异常
catch(PDOException $e){
trigger_error("数据库连接失败",E_USER_WARNING);
return false;
}
}
//用于查询的方法
public function query($sql=''){
if(!($this->pdo instanceof PDO)){
return false;
}
$result=$this->pdo->query($sql);
if(false==$result){
$error_info=$this->pdo->errorInfo();
$error_str="执行失败".$sql.'----'.$error_info[2];
trigger_error($error_str,E_USER_WARNING);
return false;
}else{
$this->result=$result;
return $result;
}
}
//用于非查询的方法
public function execu($sql=''){
if(!($this->pdo instanceof PDO)){
return false;
}
$result=$this->pdo->exec($sql);
if(false===$result){
$error_info=$this->pdo->errorInfo();
$error_str="执行失败".$sql.'----'.$error_info[2];
trigger_error($error_str,E_USER_WARNING);
return false;
}else{
$this->affected_row=$result;
return $result;
}
}
//查询所有的记录
public function fetchAll($sql=''){
$result=$this->query($sql);
if(false==$result){
return false;
}
$rows=$result->fetchAll(PDO::FETCH_ASSOC);
$result->closeCursor();
return $rows;
}
//查询一条记录
public function fetchRow($sql=''){
$result=$this->query($sql);
if(false==$result){
return false;
}
$row=$result->fetch(PDO::FETCH_ASSOC);
$result->closeCursor();
return $row;
}
//查询某条记录第一个字段
public function fetchOne($sql=''){
$result=$this->query($sql);
if(false==$result){
return false;
}
$row_one=$result->fetchColumn(4);
$result->closeCursor();
return $row_one;
}
//查询某个字段的全部数据
public function fColumn($sql=''){
$result = $this->query($sql);
$rows = $result->fetchAll(PDO::FETCH_COLUMN);
return $rows;
}
//用于提供转义的方法
public function escapeData($data=''){
return $this->pdo->quote($data);
}
//获取被影响的记录数
public function affectedRow(){
$affected_row=$this->affected_row;
return $affected_row;
}
//获取上次结果影响的记录数
public function resultRow(){
$result_row=$this->result->rowCount();
$this->result=null;
return $result_row;
}
//获取最新自动生成的ID
public function lastInsertId(){
return $this->pdo->lastInsertId();
}
}
?>
一萧一剑走江湖,一笑一乐看世界,一切美好的事物我们都需要去用心感受,聆听自然给予我们的欢乐.
小时候觉得长大了多好,可以到外面的世界看一看,可长大了,却向往童年般的生活,没有烦恼,该哭的时候哭,该笑的时候笑,想做什么都可以.即便我们长大了更应该热爱生活懂得爱自己,不要抱怨生活对你的不公,开心的活着比什么都好。
所以在这个有限的时间里,我们需要快乐高兴的活着,活出属于自己的青春.忘记那些不痛快的事情.我们需要梦想来使得我们的人生更有意义.
今天就分享到这里吧,大家可以叫我一萧,可能在以后的日子里,我分享的不仅仅是代码上的事情,因为能让我们感到快乐和高兴并且获得收获的不仅仅是技术层面上的,应该有很多很多

浙公网安备 33010602011771号