PHP 資料庫連接工具類(MySQLI函數封裝)

來源:互聯網
上載者:User

標籤:

====================mysql=====================

<?phpclass mysql{    private $mysqli;    private $result;    /**     * 資料庫連接     * @param $config 配置數組     */    public function connect($config)    {        $host = $config[‘host‘];        //主機地址        $username = $config[‘username‘];//使用者名稱        $password = $config[‘password‘];//密碼        $database = $config[‘database‘];//資料庫        $port = $config[‘port‘];        //連接埠號碼        $this->mysqli = new mysqli($host, $username, $password, $database, $port);    }    /**     * 資料查詢     * @param $table 資料表     * @param null $field 欄位     * @param null $where 條件     * @return mixed 查詢結果數目     */    public function select($table, $field = null, $where = null)    {        $sql = "SELECT * FROM {$table}";        if (!empty($field)) {            $field = ‘`‘ . implode(‘`,`‘, $field) . ‘`‘;            $sql = str_replace(‘*‘, $field, $sql);        }        if (!empty($where)) {            $sql = $sql . ‘ WHERE ‘ . $where;        }        $this->result = $this->mysqli->query($sql);        return $this->result->num_rows;    }    /**     * @return mixed 擷取全部結果     */    public function fetchAll()    {        return $this->result->fetch_all(MYSQLI_ASSOC);    }    /**     * 插入資料     * @param $table 資料表     * @param $data 資料數組     * @return mixed 插入ID     */    public function insert($table, $data)    {        foreach ($data as $key => $value) {            $data[$key] = $this->mysqli->real_escape_string($value);        }        $keys = ‘`‘ . implode(‘`,`‘, array_keys($data)) . ‘`‘;        $values = ‘\‘‘ . implode("‘,‘", array_values($data)) . ‘\‘‘;        $sql = "INSERT INTO {$table}( {$keys} )VALUES( {$values} )";        $this->mysqli->query($sql);        return $this->mysqli->insert_id;    }    /**     * 更新資料     * @param $table 資料表     * @param $data 資料數組     * @param $where 過濾條件     * @return mixed 受影響記錄     */    public function update($table, $data, $where)    {        foreach ($data as $key => $value) {            $data[$key] = $this->mysqli->real_escape_string($value);        }        $sets = array();        foreach ($data as $key => $value) {            $kstr = ‘`‘ . $key . ‘`‘;            $vstr = ‘\‘‘ . $value . ‘\‘‘;            array_push($sets, $kstr . ‘=‘ . $vstr);        }        $kav = implode(‘,‘, $sets);        $sql = "UPDATE {$table} SET {$kav} WHERE {$where}";        $this->mysqli->query($sql);        return $this->mysqli->affected_rows;    }    /**     * 刪除資料     * @param $table 資料表     * @param $where 過濾條件     * @return mixed 受影響記錄     */    public function delete($table, $where)    {        $sql = "DELETE FROM {$table} WHERE {$where}";        $this->mysqli->query($sql);        return $this->mysqli->affected_rows;    }}
mysql.class.php

====================使用方法=====================

<?phprequire_once ‘mysql.class.php‘;/* 配置串連參數 */$config = array(    ‘type‘ => ‘mysql‘,    ‘host‘ => ‘localhost‘,    ‘username‘ => ‘woider‘,    ‘password‘ => ‘3243‘,    ‘database‘ => ‘php‘,    ‘port‘ => ‘3306‘);/* 串連資料庫 */$mysql = new mysql();$mysql->connect($config);/* 查詢資料 *///1、查詢所有資料$table = ‘mysqli‘;//資料表$num = $mysql->select($table);echo ‘共查詢到‘ . $num . ‘條資料‘;print_r($mysql->fetchAll());//2、查詢部分資料$field = array(‘username‘, ‘password‘); //過濾欄位$where = ‘id % 2 =0‘;                   //過濾條件$mysql->select($table, $field, $where);print_r($mysql->fetchAll());/* 插入資料 */$table = ‘mysqli‘;//資料表$data = array(    //資料數組    ‘username‘ => ‘admin‘,    ‘password‘ => sha1(‘admin‘));$id = $mysql->insert($table, $data);echo ‘插入記錄的ID為‘ . $id;/* 修改資料 */$table = ‘mysqli‘;//資料表$data = array(    ‘password‘ => sha1(‘nimda‘));$where = ‘id = 44‘;$rows = $mysql->update($table, $data, $where);echo ‘受影響的記錄數量為‘ . $rows . ‘條‘;/* 刪除資料 */$table = ‘mysqli‘;$where = ‘id = 45‘;$rows = $mysql->delete($table, $where);echo ‘已刪除‘ . $rows . ‘條資料‘;

 

PHP 資料庫連接工具類(MySQLI函數封裝)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.