php封装PDO类


	class pdoManager
	{
	    private $conn = null;
	
	    /**
	     * 创建数据库链接对象
	     */
	    public function __construct($host,$user,$pwd){
	        try{
	            $this->conn = new PDO($host,$user,$pwd);
	            $this->conn->query('set names utf8');
	            $this->conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
	        }catch (PDOException $e){
	            die('err:'.$e->getMessage());
	        }
	    }
	
	    /**
	     * 增删改方法
	     */
	    public function toExec($sql){
	        try{
	            $result = $this->conn->exec($sql);
	            return $result;
	        }catch (PDOException $e){
	            die('err:'.$e->getMessage());
	        }
	    }
	
	    /**
	     * 数据查询方法
	     */
	    public function getData($sql,$isMore = true){
	        try{
	            $result = $this->conn->query($sql);
	            if($isMore){
	                $content = $result->fetchAll(PDO::FETCH_ASSOC);
	            }else{
	                $content = $result->fetch(PDO::FETCH_ASSOC);
	            }
	            return $content;
	        }catch (PDOException $e){
	            die('err:'.$e->getMessage());
	        }
	    }
	
	    /**
	     * 释放PDO对象
	     */
	    public function __destruct(){
	        $this->conn = null;
	    }
	}
	

猜你喜欢

转载自blog.csdn.net/qq_39043923/article/details/88685543