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

php by 黃業(yè)興 at 2020-03-27

最近升級php7發(fā)現不支持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參數與之相反和第二個參數可以不填寫!

請關注我們微信公眾號:mw748219