当前位置:Gxlcms > PHP教程 > PHP基于MySQLI函数的连接数据库类

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

时间:2021-07-01 10:21:17 帮助过:21人阅读

这篇文章主要介绍了PHP基于MySQLI函数封装的数据库连接工具类,结合实例形式分析了php封装mysqli函数实现的数据库操作类定义及连接、增删改查数据库等基本操作用法,需要的朋友可以参考下

本文实例讲述了PHP基于MySQLI函数封装的数据库连接工具类。分享给大家供大家参考,具体如下:

mysql.class.php:


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

使用方法


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

以上就是PHP基于MySQLI函数的连接数据库类的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行