当前位置:Gxlcms > PHP教程 > MYSQLI操作类

MYSQLI操作类

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

  1. <?php
  2. /**
  3. * Mysqli类
  4. *
  5. * @author 废墟
  6. * @version v1.0 2009-08-18
  7. * @link http://anerg.cn/
  8. */
  9. class db_mysqli {
  10. protected $mysqli;
  11. protected $sql;
  12. protected $rs;
  13. protected $query_num = 0;
  14. protected $fetch_mode = MYSQLI_ASSOC;
  15. protected $cache_dir = './cache/';
  16. protected $cache_time = 1800;
  17. public function __construct($dbhost, $dbuser, $dbpass, $dbname) {
  18. $this->mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
  19. if(mysqli_connect_errno()) {
  20. $this->mysqli = false;
  21. echo '<h2>'.mysqli_connect_error().'</h2>';
  22. die();
  23. } else {
  24. $this->mysqli->set_charset("utf8");
  25. }
  26. }
  27. public function __destruct() {
  28. $this->free();
  29. $this->close();
  30. }
  31. protected function free() {
  32. @$this->rs->free();
  33. }
  34. protected function close() {
  35. $this->mysqli->close();
  36. }
  37. protected function fetch() {
  38. return $this->rs->fetch_array($this->fetch_mode);
  39. }
  40. protected function getQuerySql($sql, $limit = null) {
  41. if (@preg_match("/[0-9]+(,[ ]?[0-9]+)?/is", $limit) && !preg_match("/ LIMIT [0-9]+(,[ ]?[0-9]+)?$/is", $sql)) {
  42. $sql .= " LIMIT " . $limit;
  43. }
  44. return $sql;
  45. }
  46. protected function get_cache($sql,$method) {
  47. include_once './cache.php';//若框架中使用__autoload(),这里可以不用加载文件
  48. $cache = new cache($this->cache_dir,$this->cache_time);
  49. $cache_file = md5($sql.$method);
  50. $res = $cache->get_cache($cache_file);
  51. if(!$res) {
  52. $res = $this->$method($sql);
  53. $cache->set_cache($cache_file, $res);
  54. }
  55. return $res;
  56. }
  57. public function query_num() {
  58. return $this->query_num;
  59. }
  60. public function set_cache_dir($cache_dir) {
  61. $this->cache_dir = $cache_dir;
  62. }
  63. public function set_cache_time($cache_time) {
  64. $this->cache_time = $cache_time;
  65. }
  66. public function query($sql, $limit = null) {
  67. $sql = $this->getQuerySql($sql, $limit);
  68. $this->sql = $sql;
  69. $this->rs = $this->mysqli->query($sql);
  70. if (!$this->rs) {
  71. echo "<h2>".$this->mysqli->error."</h2>";
  72. die();
  73. } else {
  74. $this->query_num++;
  75. return $this->rs;
  76. }
  77. }
  78. public function getOne($sql) {
  79. $this->query($sql, 1);
  80. $this->fetch_mode = MYSQLI_NUM;
  81. $row = $this->fetch();
  82. $this->free();
  83. return $row[0];
  84. }
  85. public function get_one($sql) {
  86. return $this->getOne($sql);
  87. }
  88. public function cache_one($sql) {
  89. $sql = $this->getQuerySql($sql, 1);
  90. return $this->get_cache($sql, 'getOne');
  91. }
  92. public function getRow($sql, $fetch_mode = MYSQLI_ASSOC) {
  93. $this->query($sql, 1);
  94. $this->fetch_mode = $fetch_mode;
  95. $row = $this->fetch();
  96. $this->free();
  97. return $row;
  98. }
  99. public function get_row($sql, $fetch_mode = MYSQLI_ASSOC) {
  100. return $this->getRow($sql);
  101. }
  102. public function cache_row($sql) {
  103. $sql = $this->getQuerySql($sql, 1);
  104. return $this->get_cache($sql, 'getRow');
  105. }
  106. public function getAll($sql, $limit = null, $fetch_mode = MYSQLI_ASSOC) {
  107. $this->query($sql, $limit);
  108. $all_rows = array();
  109. $this->fetch_mode = $fetch_mode;
  110. while($rows = $this->fetch()) {
  111. $all_rows[] = $rows;
  112. }
  113. $this->free();
  114. return $all_rows;
  115. }
  116. public function get_all($sql, $limit = null, $fetch_mode = MYSQLI_ASSOC) {
  117. return $this->getAll($sql);
  118. }
  119. public function cache_all($sql, $limit = null) {
  120. $sql = $this->getQuerySql($sql, $limit);
  121. return $this->get_cache($sql, 'getAll');
  122. }
  123. public function insert_id() {
  124. return $this->mysqli->insert_id();
  125. }
  126. public function escape($str) {
  127. if(is_array($str)) {
  128. foreach($str as $key=>$val) {
  129. $str[$key] = $this->escape($val);
  130. }
  131. } else {
  132. $str = addslashes(trim($str));
  133. }
  134. return $str;
  135. }
  136. }
  137. //用法
  138. $db = new db_mysqli('localhost', 'root', 111222, 'dict');
  139. $db->set_cache_time(10);
  140. $db->set_cache_dir('./cache/sql/');
  141. $sql = "select * from words order by word_id limit 10,10";
  142. $res1 = $db->get_all($sql);
  143. $res2 = $db->cache_all($sql);
  144. echo $db->query_num(),'<br>';
  145. ?>

mysqli类的对象主要控制PHP和MySQL数据库服务器之间的连接、选择数据库、向MySQL服务器发送SQL语句,以及设置字符集等,这些 任务都是通过该类中声明的构造方法、成员方法和成员属性完成的。在表13-1和表13-2两个表格中,分别列出了mysqli类中声明的成员方法和成员属 性。

表13-1 mysqli类中的成员方法(共33个)

QQ截图20161124093110.png

QQ截图20161124093123.png

QQ截图20161124093140.png

人气教程排行