db.class.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /*
  3. [UCenter] (C)2001-2099 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: db.class.php 1152 2013-05-28 09:11:04Z kamichen $
  6. */
  7. class ucserver_db {
  8. var $querynum = 0;
  9. var $link;
  10. var $histories;
  11. var $dbhost;
  12. var $dbuser;
  13. var $dbpw;
  14. var $dbcharset;
  15. var $pconnect;
  16. var $tablepre;
  17. var $time;
  18. var $goneaway = 5;
  19. function connect($dbhost, $dbuser, $dbpw, $dbname = '', $dbcharset = '', $pconnect = 0, $tablepre='', $time = 0) {
  20. $this->dbhost = $dbhost;
  21. $this->dbuser = $dbuser;
  22. $this->dbpw = $dbpw;
  23. $this->dbname = $dbname;
  24. $this->dbcharset = $dbcharset;
  25. $this->pconnect = $pconnect;
  26. $this->tablepre = $tablepre;
  27. $this->time = $time;
  28. if($pconnect) {
  29. if(!$this->link = mysql_pconnect($dbhost, $dbuser, $dbpw)) {
  30. $this->halt('Can not connect to MySQL server');
  31. }
  32. } else {
  33. if(!$this->link = mysql_connect($dbhost, $dbuser, $dbpw)) {
  34. $this->halt('Can not connect to MySQL server');
  35. }
  36. }
  37. if($this->version() > '4.1') {
  38. if($dbcharset) {
  39. mysql_query("SET character_set_connection=".$dbcharset.", character_set_results=".$dbcharset.", character_set_client=binary", $this->link);
  40. }
  41. if($this->version() > '5.0.1') {
  42. mysql_query("SET sql_mode=''", $this->link);
  43. }
  44. }
  45. if($dbname) {
  46. mysql_select_db($dbname, $this->link);
  47. }
  48. }
  49. function fetch_array($query, $result_type = MYSQL_ASSOC) {
  50. return mysql_fetch_array($query, $result_type);
  51. }
  52. function result_first($sql) {
  53. $query = $this->query($sql);
  54. return $this->result($query, 0);
  55. }
  56. function fetch_first($sql) {
  57. $query = $this->query($sql);
  58. return $this->fetch_array($query);
  59. }
  60. function fetch_all($sql, $id = '') {
  61. $arr = array();
  62. $query = $this->query($sql);
  63. while($data = $this->fetch_array($query)) {
  64. $id ? $arr[$data[$id]] = $data : $arr[] = $data;
  65. }
  66. return $arr;
  67. }
  68. function cache_gc() {
  69. $this->query("DELETE FROM {$this->tablepre}sqlcaches WHERE expiry<$this->time");
  70. }
  71. function query($sql, $type = '', $cachetime = FALSE) {
  72. $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query';
  73. if(!($query = $func($sql, $this->link)) && $type != 'SILENT') {
  74. $this->halt('MySQL Query Error', $sql);
  75. }
  76. $this->querynum++;
  77. $this->histories[] = $sql;
  78. return $query;
  79. }
  80. function affected_rows() {
  81. return mysql_affected_rows($this->link);
  82. }
  83. function error() {
  84. return (($this->link) ? mysql_error($this->link) : mysql_error());
  85. }
  86. function errno() {
  87. return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
  88. }
  89. function result($query, $row) {
  90. $query = @mysql_result($query, $row);
  91. return $query;
  92. }
  93. function num_rows($query) {
  94. $query = mysql_num_rows($query);
  95. return $query;
  96. }
  97. function num_fields($query) {
  98. return mysql_num_fields($query);
  99. }
  100. function free_result($query) {
  101. return mysql_free_result($query);
  102. }
  103. function insert_id() {
  104. return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
  105. }
  106. function fetch_row($query) {
  107. $query = mysql_fetch_row($query);
  108. return $query;
  109. }
  110. function fetch_fields($query) {
  111. return mysql_fetch_field($query);
  112. }
  113. function version() {
  114. return mysql_get_server_info($this->link);
  115. }
  116. function escape_string($str) {
  117. return mysql_escape_string($str);
  118. }
  119. function close() {
  120. return mysql_close($this->link);
  121. }
  122. function halt($message = '', $sql = '') {
  123. $error = mysql_error();
  124. $errorno = mysql_errno();
  125. if($errorno == 2006 && $this->goneaway-- > 0) {
  126. $this->connect($this->dbhost, $this->dbuser, $this->dbpw, $this->dbname, $this->dbcharset, $this->pconnect, $this->tablepre, $this->time);
  127. $this->query($sql);
  128. } else {
  129. $s = '';
  130. if($message) {
  131. $s = "<b>UCenter info:</b> $message<br />";
  132. }
  133. if($sql) {
  134. $s .= '<b>SQL:</b>'.dhtmlspecialchars($sql).'<br />';
  135. }
  136. $s .= '<b>Error:</b>'.$error.'<br />';
  137. $s .= '<b>Errno:</b>'.$errorno.'<br />';
  138. $s = str_replace(UC_DBTABLEPRE, '[Table]', $s);
  139. exit($s);
  140. }
  141. }
  142. }
  143. ?>