Просмотр файла system/inc/classes/mysql.class.php

Размер файла: 3.48Kb
  1. <?php
  2. /*
  3. =============================================
  4. Движок: SHCMS Engine
  5. =============================================
  6. Название файла: Класс для работы с базой данных
  7. =============================================
  8. Official website: http://shcms.ru
  9. =============================================
  10. Данный код защищен авторскими правами
  11. =============================================
  12. */
  13. class mySQL
  14. {
  15. var $db_id = false;
  16. var $connected = false;
  17. var $query_num = 0;
  18. var $query_list = array();
  19. var $mysql_error = '';
  20. var $mysql_version = '';
  21. var $mysql_error_num = 0;
  22. var $mysql_extend = "MySQL";
  23. var $MySQL_time_taken = 0;
  24. var $query_id = false;
  25. function connect($user_db, $user_pass, $user_user, $user_localhost, $show_error=1)
  26. {
  27. if(!$this->db_id = mysql_connect($user_localhost, $user_db, $user_pass)) {
  28. if($show_error == 1) {
  29. $this->display_error(mysql_error(), mysql_errno());
  30. } else {
  31. return false;
  32. }
  33. }
  34. if(!@mysql_select_db($user_user, $this->db_id)) {
  35. if($show_error == 1) {
  36. $this->display_error(mysql_error(), mysql_errno());
  37. } else {
  38. return false;
  39. }
  40. }
  41. $this->mysql_version = mysql_get_server_info();
  42. if (version_compare($this->mysql_version, '4.1', ">=")) mysql_query("/*!40101 SET NAMES '" . COLLATE . "' */");
  43. $this->connected = true;
  44. return true;
  45. }
  46. function query($query, $show_error=true)
  47. {
  48. $time_before = $this->get_real_time();
  49. if(!$this->connected) $this->connect(DBUSER, DBPASS, DBNAME, DBHOST);
  50. if(!($this->query_id = mysql_query($query, $this->db_id) )) {
  51. $this->mysql_error = mysql_error();
  52. $this->mysql_error_num = mysql_errno();
  53. if($show_error) {
  54. $this->display_error($this->mysql_error, $this->mysql_error_num, $query);
  55. }
  56. }
  57. $this->MySQL_time_taken += $this->get_real_time() - $time_before;
  58. $this->query_num ++;
  59. return $this->query_id;
  60. }
  61. function get_row($query_id = '')
  62. {
  63. if ($query_id == '') $query_id = $this->query_id;
  64. return mysql_fetch_assoc($query_id);
  65. }
  66. function get_affected_rows()
  67. {
  68. return mysql_affected_rows($this->db_id);
  69. }
  70. function get_array($query_id = '')
  71. {
  72. if ($query_id == '') $query_id = $this->query_id;
  73. return mysql_fetch_array($query_id);
  74. }
  75. function super_query($query, $multi = false)
  76. {
  77. if(!$multi) {
  78. $this->query($query);
  79. $data = $this->get_row();
  80. $this->free();
  81. return $data;
  82. } else {
  83. $this->query($query);
  84. $rows = array();
  85. while($row = $this->get_row()) {
  86. $rows[] = $row;
  87. }
  88. $this->free();
  89.  
  90. return $rows;
  91. }
  92. }
  93. function num_rows($query_id = '')
  94. {
  95. if ($query_id == '') $query_id = $this->query_id;
  96.  
  97. return mysql_num_rows($query_id);
  98. }
  99. function insert_id()
  100. {
  101. return mysql_insert_id($this->db_id);
  102. }
  103. function get_result_fields($query_id = '') {
  104.  
  105. if ($query_id == '') $query_id = $this->query_id;
  106.  
  107. while ($field = mysql_fetch_field($query_id))
  108. {
  109. $fields[] = $field;
  110. }
  111. return $fields;
  112. }
  113. function safesql( $source )
  114. {
  115.  
  116. if ($this->db_id) return mysql_real_escape_string ($source, $this->db_id);
  117. else return addslashes($source);
  118. }
  119. function free( $query_id = '' )
  120. {
  121.  
  122. if ($query_id == '') $query_id = $this->query_id;
  123.  
  124. @mysql_free_result($query_id);
  125. }
  126. function close()
  127. {
  128. @mysql_close($this->db_id);
  129. }
  130.  
  131. function get_real_time()
  132. {
  133. list($seconds, $microSeconds) = explode(' ', microtime());
  134. return ((float)$seconds + (float)$microSeconds);
  135. }
  136.  
  137. }
  138. $mySQL = new mySQL;
  139. ?>