php DBhelp

   最近学习php,学了几天,写了一个DBhelper类,贴出代码,(不足之处可以拍砖)

 

<?php
   class DBHelper{
    private $mysqli;
    private static $host='127.0.0.1';
    private static $user='root';
    private static $pwd='';
    private static $dbname='';
    
    //通过构造方法进行初始化操作
    public function __construct(){
        $this->mysqli=new mysqli(self::$host,self::$user,self::$pwd,self::$dbname)
        or die('数据库链接出错:'.$this->mysqli->connect_error);    
        //设置数据库编码为utf8
        $this->mysqli->query('set names utf8');    
    }    

    //执行查询语句
    public function execute_dml($sql){
        $arr=array();
        $result=$this->mysqli->query($sql) or die($this->mysqli->error);
        if($result){
        while($row=$result->fetch_assoc()){
            //将查询结果封装到一个数组中,返回给方法调用处
            $arr[]=$row;
        }    
        //释放查询结果资源
        $result->free();
        }    
        return $arr;
    }

    //执行统计语句
    public function total($sql){
        $result = $this->mysqli->query($sql) or die($this->mysqli->error);
        return $result->num_rows;
     }
    
    //执行增加、删除、更新语句
    public function execute_dql($sql){
        $result=$this->mysqli->query($sql) or die($this->mysqli->error);
        if(!$result){
        return 0;//表示操作失败    
        }else{
        if($this->mysqli->affected_rows>0){
            return 1;//操作成功    
        }else{
            return 2;//没有受影响的行    
        }
        }
    }
    }
?>

 

posted @ 2014-09-22 17:18  黄小兴  阅读(429)  评论(0编辑  收藏  举报