Вход Регистрация
* UndeX

Удобный класс для работы с MySQLi

  1. <?php
  2. class mysql
  3. {
  4. var $db_id = false;
  5. var $query_num = 0;
  6. var $query_list = array();
  7. var $mysql_error = '';
  8. var $mysql_version = '';
  9. var $mysql_error_num = 0;
  10. var $mysql_extend = "MySQLi";
  11. var $MySQL_time_taken = 0;
  12. var $query_id = false;
  13.  
  14.  
  15. function connect($db_user, $db_pass, $db_name, $db_location = 'localhost', $show_error=1)
  16. {
  17. $db_location = explode(":", $db_location);
  18.  
  19. if (isset($db_location[1])) {
  20.  
  21. $this->db_id = @mysqli_connect($db_location[0], $db_user, $db_pass, $db_name, $db_location[1]);
  22.  
  23. } else {
  24.  
  25. $this->db_id = @mysqli_connect($db_location[0], $db_user, $db_pass, $db_name);
  26.  
  27. }
  28.  
  29. if(!$this->db_id) {
  30. if($show_error == 1) {
  31. $this->display_error(mysqli_connect_error(), '1');
  32. } else {
  33. return false;
  34. }
  35. }
  36.  
  37. $this->mysql_version = mysqli_get_server_info($this->db_id);
  38.  
  39. if(!defined('COLLATE'))
  40. {
  41. define ("COLLATE", "cp1251");
  42. }
  43.  
  44. mysqli_query($this->db_id, "SET NAMES '" . COLLATE . "'");
  45.  
  46. return true;
  47. }
  48.  
  49. function query($query, $show_error=true)
  50. {
  51. $time_before = $this->get_real_time();
  52.  
  53. if(!$this->db_id) $this->connect(DBUSER, DBPASS, DBNAME, DBHOST);
  54.  
  55. if(!($this->query_id = mysqli_query($this->db_id, $query) )) {
  56.  
  57. $this->mysql_error = mysqli_error($this->db_id);
  58. $this->mysql_error_num = mysqli_errno($this->db_id);
  59.  
  60. if($show_error) {
  61. $this->display_error($this->mysql_error, $this->mysql_error_num, $query);
  62. }
  63. }
  64.  
  65. $this->MySQL_time_taken += $this->get_real_time() - $time_before;
  66.  
  67. $this->query_list[] = array( 'time' => ($this->get_real_time() - $time_before),
  68. 'query' => $query,
  69. 'num' => (count($this->query_list) + 1));
  70.  
  71. $this->query_num ++;
  72.  
  73. return $this->query_id;
  74. }
  75.  
  76. function fetch_assoc($query_id = '')
  77. {
  78. if ($query_id == '') $query_id = $this->query_id;
  79.  
  80. return mysqli_fetch_assoc($query_id);
  81. }
  82.  
  83. function get_affected_rows()
  84. {
  85. return mysqli_affected_rows($this->db_id);
  86. }
  87.  
  88. function fetch_array($query_id = '')
  89. {
  90. if ($query_id == '') $query_id = $this->query_id;
  91.  
  92. return mysqli_fetch_array($query_id);
  93. }
  94.  
  95. function multi_query($query, $multi = false)
  96. {
  97.  
  98. if(!$multi) {
  99.  
  100. $this->query($query);
  101. $data = $this->fetch_assoc();
  102. $this->free();
  103. return $data;
  104.  
  105. } else {
  106. $this->query($query);
  107.  
  108. $rows = array();
  109. while($row = $this->fetch_assoc()) {
  110. $rows[] = $row;
  111. }
  112.  
  113. $this->free();
  114.  
  115. return $rows;
  116. }
  117. }
  118.  
  119. function num_rows($query_id = '')
  120. {
  121. if ($query_id == '') $query_id = $this->query_id;
  122.  
  123. return mysqli_num_rows($query_id);
  124. }
  125.  
  126. function insert_id()
  127. {
  128. return mysqli_insert_id($this->db_id);
  129. }
  130.  
  131. function result($query_id = '')
  132. {
  133. if ($query_id == '') $query_id = $this->query_id;
  134.  
  135. return mysqli_result($query_id);
  136. }
  137.  
  138. function get_result_fields($query_id = '') {
  139.  
  140. if ($query_id == '') $query_id = $this->query_id;
  141.  
  142. while ($field = mysqli_fetch_field($query_id))
  143. {
  144. $fields[] = $field;
  145. }
  146.  
  147. return $fields;
  148. }
  149.  
  150. function real_escape_string( $source )
  151. {
  152. if(!$this->db_id) $this->connect(DBUSER, DBPASS, DBNAME, DBHOST);
  153.  
  154. if ($this->db_id) return mysqli_real_escape_string ($this->db_id, $source);
  155. else return addslashes($source);
  156. }
  157.  
  158. function free( $query_id = '' )
  159. {
  160.  
  161. if ($query_id == '') $query_id = $this->query_id;
  162.  
  163. @mysqli_free_result($query_id);
  164. }
  165.  
  166. function close()
  167. {
  168. @mysqli_close($this->db_id);
  169. }
  170.  
  171. function get_real_time()
  172. {
  173. list($seconds, $microSeconds) = explode(' ', microtime());
  174. return ((float)$seconds + (float)$microSeconds);
  175. }
  176.  
  177. function display_error($error, $error_num, $query = '')
  178. {
  179. if($query) {
  180. // Safify query
  181. $query = preg_replace("/([0-9a-f]){32}/", "********************************", $query); // Hides all hashes
  182. }
  183.  
  184. $query = htmlspecialchars($query, ENT_QUOTES, 'ISO-8859-1');
  185. $error = htmlspecialchars($error, ENT_QUOTES, 'ISO-8859-1');
  186.  
  187. $trace = debug_backtrace();
  188.  
  189. $level = 0;
  190. if ($trace[1]['function'] == "query" ) $level = 1;
  191. if ($trace[2]['function'] == "multi_query" ) $level = 2;
  192.  
  193. $trace[$level]['file'] = str_replace("", "", $trace[$level]['file']);
  194.  
  195. echo '
  196. <b>MySQL Error!</b><br/>
  197. <b><b>MySQL error</b> in file: <b>'.$trace[$level]['file'].'</b> at line <b>'.$trace[$level]['line'].'</b><br/>
  198. <b>Error Number: <b>'.$error_num.'</b><br/>
  199. <b>The Error returned was:<br /> <b>'.$error.'</b><br/>
  200. <b>SQL query:</b><br /><br />'.$query.'<br /><hr />';
  201.  
  202.  
  203. }
  204.  
  205. }
  206.  
  207.  
  208. ?>
» Описание: Удобный класс для работы с MySQLi
» Время добавления: 7 Сент 2016 в 09:37
» Посмотров: 3051
» textarea
» Рейтинг: [+0 | -2]
Комментарии [4]
Онлайн: 1
Реклама