php7不支持mysql扩展需要改成mysqli扩展

最近升级php7发现不支持mysql扩展,需要改成用mysqli扩展

看代码

class Db{
    private $username = '';
    private $password = '';
    private $host = '';
    private $db = '';
    private $mysqli_conn;
    
    public function __construct(){
        $this->mysqli_conn = @mysqli_connect($this->host,$this->username, $this->password);
        if(!$this->mysqli_conn){
            die("could not connect to the database:\n".mysqli_error($this->mysqli_conn));
        }
        mysqli_query($this->mysqli_conn,"set names 'utf8'");//编码转化
        $select_db = mysqli_select_db($this->mysqli_conn,$this->db);
    }
    
    public function query($query){
        return mysqli_query($this->mysqli_conn, $query);
    }
    
    public function fetch_assoc($result){
        return mysqli_fetch_assoc($result);
    }
    
    public function fetch_array($result){
        return mysqli_fetch_array($result);
    }
    
    public function getAll($table,$filed,$where='1=1',$order='',$limit=''){
        $array = array();
        $sql = "SELECT {$filed} FROM {$table} WHERE {$where} {$order} {$limit}";
        $result = $this->query($sql) or die("db.php;Action:getAll;problem:".$sql.mysqli_error($this->mysqli_conn));
        while($row = $this->fetch_assoc($result)){
            $array[] = $row;
        }
        return $array;
    }
    
    public function getOne($table,$filed,$where='1=1'){
        $sql ="SECLECT {$filed} FROM {$table} WHERE {$where} limit 1";
        $result = $this->query($sql) or die("db.php;Action:getOne;problem:".$sql.mysqli_error($this->mysqli_conn));
        $row = $this->fetch_assoc($result);
        return $row;
    }
    
    public function getCount($table,$where="1=1"){
        $sql ="SELECT COUNT(1) as count FROM {$table} WHERE {$where}";
        $result = $this->query($sql) or die("db.php;Action:getCount;".$sql.mysqli_error($this->mysqli_conn));
        $row = $this->fetch_array($result);
        if(!$row['count']){
            $row['count'] = '0';
        }
        return $row['count'];
    }
    
    public function delete($table,$where="1=1"){
        $sql ="DELETE FROM {$table} WHERE {$where}";
        $result = $this->query($sql) or die("db.php;Action:delete;".$sql.mysqli_error($this->mysqli_conn));
        return $result;
    }
    
    public function insert($table,array $data){
        $data = $this->_dataFormat($data);
        $sql = "insert into ".$table."(".implode(',',array_keys($data)).") values (".implode(',',array_values($data)).")";
        $result = $this->query($sql) or die("db.php;Action:insert;".$sql.mysqli_error($this->mysqli_conn));
        return $result;
    }
    
    public function update($table,array $data,$where="1=1") {
        $data = $this->_dataFormat($data);
        if (!$data) return;
        $valArr = '';
        foreach($data as $k=>$v){
            $valArr[] = $k.'='.$v;
        }
        $valStr = implode(',', $valArr);
        $sql = "update ".$table." set ".trim($valStr)." where {$where}";
        $result = $this->query($sql) or die("db.php;Action:update;".$sql.mysqli_error($this->mysqli_conn));
        return $result;
    }
    
    priavte function _dataFormat($data) {
        if (!is_array($data)) return array();
        $ret=array();
        foreach ($data as $key=>$val) {
            if (!is_scalar($val)) continue; //值不是标量则跳过
                $key = $this->_addChar($key);
                if (is_int($val)) {
                    $val = intval($val);
                } elseif (is_float($val)) {
                    $val = floatval($val);
                } elseif (preg_match('/^\(\w*(\+|\-|\*|\/)?\w*\)$/i', $val)) {
                    $val = $val;
                } elseif (is_string($val)) {
                    $val = '"'.addslashes($val).'"';
                }
                $ret[$key] = $val;
        }
        return $ret;
    }
    
    priavte function _addChar($value) {
        if ('*'==$value || false!==strpos($value,'(') || false!==strpos($value,'.') || false!==strpos($value,'`')) {
        } elseif (false === strpos($value,'`') ) {
            $value = '`'.trim($value).'`';
        }
        return $value;
    }
}

主要注意是mysqli_query这个函数两个参数是必须填写的,而mysql_query参数与之相反和第二个参数可以不填写!

标签:

发表评论