登录  /  注册

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

巴扎黑
发布: 2017-08-12 10:50:35
原创
1739人浏览过

这篇文章主要介绍了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中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号