Home > Backend Development > PHP Tutorial > mysqli class (no transaction precompilation)

mysqli class (no transaction precompilation)

WBOY
Release: 2016-08-08 09:28:26
Original
1130 people have browsed it
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/3/10
 * Time: 23:16
 */

class DB{
    #定义变量
    private  $host;
    private  $user;
    private  $password;
    private  $charset;
    private  $dbname;
    private  $mysqli;        #mysqlid 连接

    #定义构造函数
    /**
     * @param array $arr
     */
    public function  __construct($arr=array()){
        $this->host=isset($arr['host'])?$arr['host']:'localhost';
        $this->user=isset($arr['$password'])?$arr['user']:'root';
        $this->password=isset($arr['password'])?$arr['password']:'759114';
        $this->charset=isset($arr['charset'])?$arr['charset']:'utf8';
        $this->dbname=isset($arr['dbname'])?$arr['dbname']:'project';

        $this->mysqli= new mysqli($this->host,$this->user,$this->password,$this->dbname);

    }


    #对sql语句进行判断
    private function  exec($sql){
        if(!($this->mysqli->query($sql))){
            die($this->mysqli->error);                     #错误处理
        }
        return $this->mysqli->query($sql);
    }

    #数据的操作
    public function  db_delete($sql){
        $this->exec($sql); 
        echo "您的sql语句错误:<br/>";                         #执行exec()
        return $this->mysqli->affected_rows;        #获取受影响的行数
    }

    #数据更新操作
    public  function  db_update($sql){
        $this->exec($sql);                          #执行exec()
        return $this->mysqli->affected_rows;        #获取受影响的行数
    }

    #插入操作
    public function  db_insert($sql){
        $res = $this->exec($sql);                          #执行exec()
        return $this->mysqli->affected_rows;        #获取受影响的行数
    
    }

    #获取单条数据
    public function db_getOne($sql){
        $res = $this->exec($sql);
        return $res = $res->fetch_assoc();
    }

    #获取多条数据
    public function db_getAll($sql){
        $res = $this->exec($sql);
        return $res->fetch_all();
    }

}
Copy after login

The above introduces the mysqli class (without transaction precompilation), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template