====================mysql=====================

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

mysql.class.php

====================使用方法=====================

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

PHP 数据库连接工具类(MySQLI函数包装)的更多相关文章

  1. 数据库连接工具类——包含取得连接和关闭资源 ConnUtil.java

    package com.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepare ...

  2. 数据库连接工具类 数据库连接工具类——仅仅获得连接对象 ConnDB.java

    package com.util; import java.sql.Connection; import java.sql.DriverManager; /** * 数据库连接工具类——仅仅获得连接对 ...

  3. JDBC连接数据库,结合DbUtil数据库连接工具类的使用

    (以Mysql数据库为例) 第一步:在项目里配置数据库驱动 Build Path->configure  Build Path ->Add Exteral JARs   将JDBC驱动包导 ...

  4. mysql数据库连接工具类C3P0

    package com.dl.network_flow.db; import java.sql.Connection; import java.sql.PreparedStatement; impor ...

  5. mongo数据库连接工具类(C#)

    Framework版本:.Net Framework 4 using System; using System.Collections.Generic; using System.Linq; usin ...

  6. MySQL数据库操作类(PHP实现,支持连贯操作)

    <?php /** * Author: suvan * CreateTime: 2018/2/27 * description: 数据库操作类(仅对接MySQL数据库,主要利用MySQLi函数) ...

  7. 谈一下关于C++函数包装问题

    在C++中,我们经常遇到在某个特定的时刻,需要将函数进行包装调用,尤其是当我们需要将不同签名的函数放到同一个集合时,由于函数签名不一致导致我们不能直接将各式各样的函数指针放到诸如list这样的集合中, ...

  8. 工具类之数据库工具类:DBUtil(採用反射机制)

    常常操作数据库的码农们一定知道操作数据库是一项非常复杂的工作.它不仅要解决各种乱码的问题还要解决各种数据表的增删改查等的操作. 另外每次操作数据库都要用到数据库连接.运行SQL语句.关闭连接的操作.所 ...

  9. 通过使用集合Properties完成JDBC的连接工具类

    1.将数据库连接对象所需参数保存在本地文件中 database.properties driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://localh ...

随机推荐

  1. [LeetCode] First Missing Positive 首个缺失的正数

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  2. asp.net webservice返回json问题

    使用jQuery $.ajax方法请求webservice 一.方法返回值为string,将json格式的字符串返回 设置contentType为"application/json;char ...

  3. Newtonsoft.Json 序列化和反序列化 时间格式【转】

    1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg:   A a=new A(); a.Name="Elain ...

  4. Android疑难杂症收集

    在渲染前获取 View 的宽高 5种手势工具类 软键盘用法总结 Android中Shape的使用 Android只能动态注册的广播Action Android 悬浮窗权限各机型各系统适配大全 录音权限 ...

  5. HTML5学习总结-番外04 Cordova/PhoneGap

    一 PhoneGap 1 PhoneGap简绍 http://www.cnblogs.com/JustRun1983/p/3819433.html 2 环境安装 http://cordova.apac ...

  6. JSPatch 技术要点

    作者:干掉crash链接:https://zhuanlan.zhihu.com/p/21884786来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 如果有方法不遵守语言 ...

  7. HTTP Cookie详解

    1.什么是HTTP Cookie? Wikipedia给出的定义是:An HTTP cookie is a small piece of data sent from a website and st ...

  8. 命名空间jquery

    命名空间的意思就是 同一个元素有绑定了2个相同的事件,比如2个scroll,这个时候你做其他交互的时候只想触发第二个scroll事件  就可以用命名空间做了 <button id="b ...

  9. python 静态方法、类方法(二)

    <Python静态方法.类方法>一文中曾用在类之外生成函数的方式,来计算类的实例的个数.本文将探讨用静态方法和类方法来实现此功能. 一使用静态方法统计实例 例1.static.py # - ...

  10. C3P0连接池配置和实现详解

    一.配置 <c3p0-config> <default-config> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数.Default: 3 --> ...