zoukankan      html  css  js  c++  java
  • 用接口实现的一个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();
    	 }
     
    }
    
    ?>
    

      

    一萧一剑走江湖,一笑一乐看世界,一切美好的事物我们都需要去用心感受,聆听自然给予我们的欢乐. 小时候觉得长大了多好,可以到外面的世界看一看,可长大了,却向往童年般的生活,没有烦恼,该哭的时候哭,该笑的时候笑,想做什么都可以.即便我们长大了更应该热爱生活懂得爱自己,不要抱怨生活对你的不公,开心的活着比什么都好。 所以在这个有限的时间里,我们需要快乐高兴的活着,活出属于自己的青春.忘记那些不痛快的事情.我们需要梦想来使得我们的人生更有意义. 今天就分享到这里吧,大家可以叫我一萧,可能在以后的日子里,我分享的不仅仅是代码上的事情,因为能让我们感到快乐和高兴并且获得收获的不仅仅是技术层面上的,应该有很多很多
  • 相关阅读:
    Golang Failpoint 的设计与实现
    没涉及到最值求解;观点:矩阵乘法无法表达出结果。 现实生活中事件、现象的数学表达
    多元微分学 枚举破解15位路由器密码 存储空间限制 拆分减长,求最值 数据去重
    ARP Poisoning Attack and Mitigation Techniques ARP欺骗 中间人攻击 Man-In-The-Middle (MITM) attack 嗅探 防范 Can one MAC address have two different IP addresses within the network?
    The C10K problem
    HTTP Streaming Architecture HLS 直播点播 HTTP流架构
    现代IM系统中消息推送和存储架构的实现
    现代IM系统中的消息系统架构
    长连接锁服务优化实践 C10K问题 nodejs的内部构造 limits.conf文件修改 sysctl.conf文件修改
    doubleclick cookie、动态脚本、用户画像、用户行为分析和海量数据存取 推荐词 京东 电商 信息上传 黑洞 https://blackhole.m.jd.com/getinfo
  • 原文地址:https://www.cnblogs.com/ylmfg/p/5487905.html
Copyright © 2011-2022 走看看