MySQL 封装

class DbMysqlManage{
    public $message;  //操作提示信息
    public $conn;     //link资源
    public function getMessage($mes,$flag=FALSE){
        if($flag){           
            $this->message.="<div style='color:green;font-size:16px'>".$mes."</div>";
            return true;
        }else{
            $this->message.="<div style='color:red;font-size:16px'>".$mes."</div>";
            return false;
        }
    }
    /*
     *函数名:__construct()
     *功能:连接数据库服务器,选择数据库,设置连接编码
     *参数:$localhost 主机地址,$username 用户名 ,$password 密码 , $dbName 数据库名称 , $charset 编码
     */
    public function __construct($localhost,$username,$password,$dbName,$charset='utf8') {
        //数据库link
        $this->conn=@mysql_connect($localhost,$username,$password);
        if($this->conn){
             $this->getMessage('数据库连接成功',true);         
        }else{
           return $this->getMessage('数据库连接失败'.' 错误编号 ['.mysql_errno().'] 错误信息 '.mysql_error());         
        }
        //select 数据库
        $result=mysql_select_db($dbName);
        if($result){
             $this->getMessage('选择数据库成功',true);
        }else{
           return $this->getMessage('数据库选择失败'.' 错误编号 ['.mysql_errno().'] 错误信息 '.mysql_error());            
        }
        //设置数据库编码
        $res=mysql_query('set names '.$charset);
        if($res){
            return $this->getMessage('数据库设置编码成功',true);
        }else{
           return $this->getMessage('数据库设置编码失败'.' 错误编号 ['.mysql_errno().'] 错误信息 '.mysql_error());
             
        }       
    }
    /*
     *函数名:execSql()
     *功能:增insert、删delete、改update操作
     *参数:$sql sql语句
     *返回值:如果执行sql语句成功则返回true,失败返回false
     */
    public function execSql($sql){
        $sql=trim($sql);
        $result=preg_match('/^(insert|update|delete)/', $sql);
        if($result){
            //符合正则要求
            $res=mysql_query($sql);
            if($res){
             return  $this->getMessage('执行增删改语句成功',true);
            }else{
                return $this->getMessage('执行的增删改语句有误。'.' 错误编号 ['.mysql_errno().'] 错误信息 '.mysql_error());
                
            }
        }else{
           return $this->getMessage(__FUNCTION__.'方法只支持增、删、改操作');          
        }
    }
    /*
     *函数名:getOneData()
     *功能:获取一条记录
     *参数:$sql sql语句
     *返回值:如果执行sql语句成功则返回一维数组,失败返回false
     */
    public function getOneData($sql){
        $sql=trim($sql);
        $result=preg_match('/^select/', $sql);
       if($result){
           //执行查询
           $res=mysql_query($sql);
           if($res){
               $rs=mysql_fetch_array($res,MYSQL_ASSOC);
               if(mysql_fetch_array($res,MYSQL_ASSOC)===false){
                   $this->getMessage('执行单一查询成功',true);
                   mysql_free_result($res);
                   return $rs;
               }else{
                   return $this->getMessage(__FUNCTION__.'方法只支持单一查询');                   
               }
           }else{
              return  $this->getMessage($sql.'执行失败'.' 错误编号 ['.mysql_errno().'] 错误信息 '.mysql_error());               
           }
           
       }else{
           return $this->getMessage(__FUNCTION__.'方法只支持查询操作');          
       }
    }
    /*
     *函数名:getMoreData()
     *功能:获取多条记录
     *参数:$sql sql语句
     *返回值:如果执行sql语句成功则返回二维数组,失败返回false
     */
    public function getMoreData($sql){
        $sql=trim($sql);
        $match=preg_match('/^select/', $sql);
        if($match){
            $resultSets=mysql_query($sql);
            if($resultSets){
                while($res=mysql_fetch_array($resultSets,MYSQL_ASSOC)){
                    $RES[]=$res;            
                }
                $this->getMessage('查询多条数据成功',true);
                mysql_free_result($resultSets);
                return $RES;
           }else{
              return  $this->getMessage($sql.'查询有误'.' 错误编号 ['.mysql_errno().'] 错误信息 '.mysql_error());                 
            }
        }else{
            return $this->getMessage(__FUNTION__.'方法只支持查询操作');            
        }
    }
    /*
     *函数名:colsedb();
     *功能:关闭数据库连接
     *参数:无
     *返回值:bool
     */
    public function closeDb(){
        $result=mysql_close($this->conn);
        if($result){
            return $this->getMessage('成功关闭资源',true);           
        }else{
           return  $this->getMessage('关闭资源失败');          
        }
    }
    /*
     *函数名:__destruct();
     *功能:释放资源
     *参数:无
     *返回值:无
     */
    public function __destruct(){
        unset($this->message);
    }
}

posted @ 2017-05-04 15:49  皇家玄学团  阅读(207)  评论(0编辑  收藏  举报