PHP基於MySQLI函數封裝的資料庫詳解

小云云
發布: 2023-03-19 17:16:02
原創
2666 人瀏覽過

本文主要介紹了PHP基於MySQLI函數封裝的資料庫連接工具類別,結合實例形式分析了php封裝mysqli函數實現的資料庫操作類別定義及連接、增刪改查資料庫等基本操作用法,需要的朋友可以參考下,希望能幫助大家。

mysql.class.php:


<?php
class mysql
{
  private $mysqli;
  private $result;
  /**
   * 数据库连接
   * @param $config 配置数组
   */
  public function connect($config)
  {
    $host = $config[&#39;host&#39;];    //主机地址
    $username = $config[&#39;username&#39;];//用户名
    $password = $config[&#39;password&#39;];//密码
    $database = $config[&#39;database&#39;];//数据库
    $port = $config[&#39;port&#39;];    //端口号
    $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 = &#39;`&#39; . implode(&#39;`,`&#39;, $field) . &#39;`&#39;;
      $sql = str_replace(&#39;*&#39;, $field, $sql);
    }
    if (!empty($where)) {
      $sql = $sql . &#39; WHERE &#39; . $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 = &#39;`&#39; . implode(&#39;`,`&#39;, array_keys($data)) . &#39;`&#39;;
    $values = &#39;\&#39;&#39; . implode("&#39;,&#39;", array_values($data)) . &#39;\&#39;&#39;;
    $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 = &#39;`&#39; . $key . &#39;`&#39;;
      $vstr = &#39;\&#39;&#39; . $value . &#39;\&#39;&#39;;
      array_push($sets, $kstr . &#39;=&#39; . $vstr);
    }
    $kav = implode(&#39;,&#39;, $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;
  }
}
登入後複製

使用方法


<?php
require_once &#39;mysql.class.php&#39;;
/* 配置连接参数 */
$config = array(
  &#39;type&#39; => &#39;mysql&#39;,
  &#39;host&#39; => &#39;localhost&#39;,
  &#39;username&#39; => &#39;woider&#39;,
  &#39;password&#39; => &#39;3243&#39;,
  &#39;database&#39; => &#39;php&#39;,
  &#39;port&#39; => &#39;3306&#39;
);
/* 连接数据库 */
$mysql = new mysql();
$mysql->connect($config);
/* 查询数据 */
//1、查询所有数据
$table = &#39;mysqli&#39;;//数据表
$num = $mysql->select($table);
echo &#39;共查询到&#39; . $num . &#39;条数据&#39;;
print_r($mysql->fetchAll());
//2、查询部分数据
$field = array(&#39;username&#39;, &#39;password&#39;); //过滤字段
$where = &#39;id % 2 =0&#39;;          //过滤条件
$mysql->select($table, $field, $where);
print_r($mysql->fetchAll());
/* 插入数据 */
$table = &#39;mysqli&#39;;//数据表
$data = array(  //数据数组
  &#39;username&#39; => &#39;admin&#39;,
  &#39;password&#39; => sha1(&#39;admin&#39;)
);
$id = $mysql->insert($table, $data);
echo &#39;插入记录的ID为&#39; . $id;
/* 修改数据 */
$table = &#39;mysqli&#39;;//数据表
$data = array(
  &#39;password&#39; => sha1(&#39;nimda&#39;)
);
$where = &#39;id = 44&#39;;
$rows = $mysql->update($table, $data, $where);
echo &#39;受影响的记录数量为&#39; . $rows . &#39;条&#39;;
/* 删除数据 */
$table = &#39;mysqli&#39;;
$where = &#39;id = 45&#39;;
$rows = $mysql->delete($table, $where);
echo &#39;已删除&#39; . $rows . &#39;条数据&#39;;
登入後複製

相關推薦:

javascript建立物件、函數封裝、屬性程式碼實例詳解

#日期函數和對於函數封裝的彈性運用

###################################### ##javascript建立物件、函數封裝、屬性程式碼實例詳解######

以上是PHP基於MySQLI函數封裝的資料庫詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!