Home  >  Article  >  Backend Development  >  Detailed explanation of PHP database based on MySQLI function encapsulation

Detailed explanation of PHP database based on MySQLI function encapsulation

小云云
小云云Original
2018-01-26 14:25:092721browse

This article mainly introduces the database connection tool class encapsulated by PHP based on the MySQLI function. It analyzes the database operation class definition and basic operation usage such as connection, addition, deletion, modification and query of the database implemented by the PHP encapsulation mysqli function in the form of examples. Friends in need can refer to it. Next, I hope it can help everyone.

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

Usage


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

Related recommendations:

Detailed explanation of Javascript creation objects, function encapsulation, and attribute code examples

Date functions and flexible use of function encapsulation

javascript Detailed explanation of object creation, function encapsulation, and attribute code examples

The above is the detailed content of Detailed explanation of PHP database based on MySQLI function encapsulation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn