首页 >后端开发 >php教程 >PHP基于MySQLI函数的连接数据库类

PHP基于MySQLI函数的连接数据库类

巴扎黑
巴扎黑原创
2017-08-12 10:50:351989浏览

这篇文章主要介绍了PHP基于MySQLI函数封装的数据库连接工具类,结合实例形式分析了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;;

以上是PHP基于MySQLI函数的连接数据库类的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn