background image

        }
    }
 
    //关闭资源
    $mysqli->close();
?>
3、进行封装
<?
class SqlHelper{        
        private $mysqli;
        //这里先写死,以后写死的东西用一个文件来配置
        private static $host="localhost";
        private static $user="root";
        private static $pwd="root";
        private static $db="test";
 
        public function __construct(){
            
            $this->mysqli=new MySQLi(self::$host,self::$user,self::$pwd,self::$db);
            if($this->mysqli->connect_error){
                die("连接失败".$this->mysqli->connect_error);
            }
            //设置字符集
            $this->mysqli->query("set names utf8");
        }
 
        //dql operate
        function execute_dql($sql){
 
            $res =$this->mysqli->query($sql) or die($this->mysqli->error);
            return $res;        
        }
 
        //dml operate
        function execute_dml($sql){
 
            $res =$this->mysqli->query($sql) or die($this->mysqli->error);
            
            if(!$res){
                return 0;//失败
            }else{
                if($this->mysqli->affected_rows>0){
                    return 1;//成功
                }else{