国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP基于MySQLI函數封裝的數據庫連接工具類【定義與用法】

2024-05-04 23:59:24
字體:
來源:轉載
供稿:網友

本文實例講述了PHP基于MySQLI函數封裝的數據庫連接工具類。分享給大家供大家參考,具體如下:

mysql.class.php:

<?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;  }}

使用方法

<?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程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 搜索| 宁国市| 宜宾市| 石阡县| 西乌珠穆沁旗| 五原县| 湖口县| 霍林郭勒市| 灵璧县| 汉沽区| 通道| 云林县| 赤城县| 遂川县| 阿坝县| 和政县| 如皋市| 宣汉县| 乐业县| 遂昌县| 阿尔山市| 漾濞| 扶余县| 永昌县| 公主岭市| 吉首市| 琼结县| 晋州市| 岢岚县| 乐业县| 冷水江市| 安泽县| 盘锦市| 定西市| 石屏县| 吉安市| 三台县| 鹿泉市| 定兴县| 武义县| 徐闻县|