• 全都有综合资源网

    分享赚钱

    一个专业的免费资源网

    每天更新100+优质资源

  • 手机版二维码

    随时手机查素材

  • 扫描二维码

    加入官方微信群

PHP基于MySQLI函数封装的数据库连接工具类的使用方法详解

技术文章 每日更新
2024-6-17 09:31 165人浏览 0人回复
原作者: 全都有综合资源网 来自: 全都有综合资源网 收藏 分享 邀请
摘要

本文重点研究了 PHP 中包装在 MySQLI 函数中的数据库连接实用程序类,分析了 PHP 包 MySQLI 函数中数据库操作类的定义和连接,以及数据库的添加、删除和检查等。 本文描述了一个用MySQLI函数封装PHP数据库连接工具类 ...

  本文重点研讨了 PHP 中包装在 MySQLI 函数中的数据库毗连适用法式类,分析了 PHP 包 MySQLI 函数中数据库操纵类的界说和毗连,以及数据库的增加、删除和检查等。
本文描写了一个用MySQLI函数封装PHP数据库毗连工具类的实例。分享给你,供你参考,以下:

PHP基于MySQLI函数封装的数据库毗连工具类的利用方式详解2446 作者:全都有综合资本网 来历:全都有综合资本网 公布时候:2024-6-17 09:31

mysql.class.php:

layui-box layui-code-view" style="margin-top: 10px; margin-bottom: 10px; padding: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow-wrap: break-word; box-sizing: content-box; position: relative; font-size: 12px; border-width: 1px 1px 1px 6px; border-style: solid; border-color: rgb(226, 226, 226); border-image: initial; background-color: rgb(242, 242, 242); color: rgb(51, 51, 51); font-family: "Courier New";">

code

  1. class mysql
  2. {
  3.   private $mysqli;
  4.   private $result;
  5.   /**
  6.    * 数据库毗连
  7.    * @param $config 设置数组
  8.    */
  9.   public function connect($config)
  10.   {
  11.     $host = $config['host'];    //主机地址
  12.     $username = $config['username'];//用户名
  13.     $password = $config['password'];//密码
  14.     $database = $config['database'];//数据库
  15.     $port = $config['port'];    //端口号
  16.     $this->mysqli = new mysqli($host, $username, $password, $database, $port);
  17.   }
  18.   /**
  19.    * 数据查询
  20.    * @param $table 数据表
  21.    * @param null $field 字段
  22.    * @param null $where 条件
  23.    * @return mixed 查询成果数目
  24.    */
  25.   public function select($table, $field = null, $where = null)
  26.   {
  27.     $sql = "SELECT * FROM {$table}";
  28.     if (!empty($field)) {
  29.       $field = '`' . implode('`,`', $field) . '`';
  30.       $sql = str_replace('*', $field, $sql);
  31.     }
  32.     if (!empty($where)) {
  33.       $sql = $sql . ' WHERE ' . $where;
  34.     }
  35.     $this->result = $this->mysqli->query($sql);
  36.     return $this->result->num_rows;
  37.   }
  38.   /**
  39.    * @return mixed 获得全数成果
  40.    */
  41.   public function fetchAll()
  42.   {
  43.     return $this->result->fetch_all(MYSQLI_ASSOC);
  44.   }
  45.   /**
  46.    * 插入数据
  47.    * @param $table 数据表
  48.    * @param $data 数据数组
  49.    * @return mixed 插入ID
  50.    */
  51.   public function insert($table, $data)
  52.   {
  53.     foreach ($data as $key => $value) {
  54.       $data[$key] = $this->mysqli->real_escape_string($value);
  55.     }
  56.     $keys = '`' . implode('`,`', array_keys($data)) . '`';
  57.     $values = '\'' . implode("','", array_values($data)) . '\'';
  58.     $sql = "INSERT INTO {$table}( {$keys} )VALUES( {$values} )";
  59.     $this->mysqli->query($sql);
  60.     return $this->mysqli->insert_id;
  61.   }
  62.   /**
  63.    * 更新数据
  64.    * @param $table 数据表
  65.    * @param $data 数据数组
  66.    * @param $where 过滤条件
  67.    * @return mixed 受影响记录
  68.    */
  69.   public function update($table, $data, $where)
  70.   {
  71.     foreach ($data as $key => $value) {
  72.       $data[$key] = $this->mysqli->real_escape_string($value);
  73.     }
  74.     $sets = array();
  75.     foreach ($data as $key => $value) {
  76.       $kstr = '`' . $key . '`';
  77.       $vstr = '\'' . $value . '\'';
  78.       array_push($sets, $kstr . '=' . $vstr);
  79.     }
  80.     $kav = implode(',', $sets);
  81.     $sql = "UPDATE {$table} SET {$kav} WHERE {$where}";
  82.     $this->mysqli->query($sql);
  83.     return $this->mysqli->affected_rows;
  84.   }
  85.   /**
  86.    * 删除数据
  87.    * @param $table 数据表
  88.    * @param $where 过滤条件
  89.    * @return mixed 受影响记录
  90.    */
  91.   public function delete($table, $where)
  92.   {
  93.     $sql = "DELETE FROM {$table} WHERE {$where}";
  94.     $this->mysqli->query($sql);
  95.     return $this->mysqli->affected_rows;
  96.   }
  97. }


利用方式:

code

  1. require_once 'mysql.class.php';
  2. /* 设置毗连参数 */
  3. $config = array(
  4.   'type' => 'mysql',
  5.   'host' => 'localhost',
  6.   'username' => 'woider',
  7.   'password' => '3243',
  8.   'database' => 'php',
  9.   'port' => '3306'
  10. );
  11. /* 毗连数据库 */
  12. $mysql = new mysql();
  13. $mysql->connect($config);
  14. /* 查询数据 */
  15. //1、查询所稀有据
  16. $table = 'mysqli';//数据表
  17. $num = $mysql->select($table);
  18. echo '共查询到' . $num . '条数据';
  19. print_r($mysql->fetchAll());
  20. //2、查询部分数据
  21. $field = array('username', 'password'); //过滤字段
  22. $where = 'id % 2 =0';          //过滤条件
  23. $mysql->select($table, $field, $where);
  24. print_r($mysql->fetchAll());
  25. /* 插入数据 */
  26. $table = 'mysqli';//数据表
  27. $data = array(  //数据数组
  28.   'username' => 'admin',
  29.   'password' => sha1('admin')
  30. );
  31. $id = $mysql->insert($table, $data);
  32. echo '插入记录的ID为' . $id;
  33. /* 点窜数据 */
  34. $table = 'mysqli';//数据表
  35. $data = array(
  36.   'password' => sha1('nimda')
  37. );
  38. $where = 'id = 44';
  39. $rows = $mysql->update($table, $data, $where);
  40. echo '受影响的记录数目为' . $rows . '条';
  41. /* 删除数据 */
  42. $table = 'mysqli';
  43. $where = 'id = 45';
  44. $rows = $mysql->delete($table, $where);
  45. echo '已删除' . $rows . '条数据';


©版权免责声明
1、本站所有资源均来自用户上传及互联网。 如有侵权,请联系站长!
2、分享目的仅供大家学习交流。 下载后必须在24小时内删除!
3、不得用于非法商业目的或违反国家法律。 否则,后果自负!
4、本站提供的源代码、模板、插件等资源不包含技术服务。 敬请谅解!
5.如果出现无法下载、无效或有广告的链接,请联系管理员寻求帮助!
6、本站资源价格仅用于赞助,所收取的费用仅用于维持本站日常运营!
7、如果遇到加密压缩包,请使用WINRAR解压。 如果遇到无法解压的加密压缩包,请联系管理员!
8、由于精力有限,很多源代码无法详细测试(解密),部分源代码无法区分为病毒或误报,所以没有进行修改。 请在使用前进行筛选。

路过

雷人

握手

鲜花

鸡蛋
热门教程
专业的免费源码资源分享平台
每天更新100+资源

招募版主发工资

  • 官方在线客服

    QQ客服:红颜

    点击交谈

    在线客服:良子

    点击交谈

    在线客服:闵月

    点击交谈
  • 上海市虹口区海伦中心B座4F4055-4056室

  • 手机扫码查看手机版

    手机查找资源更方便

  • 扫一扫关注官方微信公众号

    加入官方微信

一个专业的免费源码资源互联网分享平台 ©2001-2024 https://www.douyouvip.com全都有综合资源网( 豫ICP备2024057239号-1 )赞助会员|网站地图 HTML
全都有综合资源网,WordPress主题PHP源码,PHP网站源码,网站模板,软件源码,网站源码免费下载,免费网站源码,网站源码模板,免费网站源码下载,wp免费源码,wp免费主题下载,PHP企业网站源码,软件源码下载