db.class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 1171 2014-11-03 03:33:47Z hypowang $
  6. */
  7. class ucclient_db
  8. {
  9. var $querynum = 0;
  10. var $link;
  11. var $histories;
  12. var $dbhost;
  13. var $dbuser;
  14. var $dbpw;
  15. var $dbcharset;
  16. var $pconnect;
  17. var $tablepre;
  18. var $time;
  19. var $goneaway = 5;
  20. function connect($dbhost, $dbuser, $dbpw, $dbname = '', $dbcharset = '', $pconnect = 0, $tablepre = '', $time = 0)
  21. {
  22. $this->dbhost = $dbhost;
  23. $this->dbuser = $dbuser;
  24. $this->dbpw = $dbpw;
  25. $this->dbname = $dbname;
  26. $this->dbcharset = $dbcharset;
  27. $this->pconnect = $pconnect;
  28. $this->tablepre = $tablepre;
  29. $this->time = $time;
  30. if ($pconnect)
  31. {
  32. if (!$this->link = mysql_pconnect($dbhost, $dbuser, $dbpw))
  33. {
  34. $this->halt('Can not connect to MySQL server');
  35. }
  36. }
  37. else
  38. {
  39. if (!$this->link = mysql_connect($dbhost, $dbuser, $dbpw))
  40. {
  41. $this->halt('Can not connect to MySQL server');
  42. }
  43. }
  44. if ($this->version() > '4.1')
  45. {
  46. if ($dbcharset)
  47. {
  48. mysql_query("SET character_set_connection=" . $dbcharset . ", character_set_results=" . $dbcharset . ", character_set_client=binary", $this->link);
  49. }
  50. if ($this->version() > '5.0.1')
  51. {
  52. mysql_query("SET sql_mode=''", $this->link);
  53. }
  54. }
  55. if ($dbname)
  56. {
  57. mysql_select_db($dbname, $this->link);
  58. }
  59. }
  60. function fetch_array($query, $result_type = MYSQL_ASSOC)
  61. {
  62. return mysql_fetch_array($query, $result_type);
  63. }
  64. function result_first($sql)
  65. {
  66. $query = $this->query($sql);
  67. return $this->result($query, 0);
  68. }
  69. function fetch_first($sql)
  70. {
  71. $query = $this->query($sql);
  72. return $this->fetch_array($query);
  73. }
  74. function fetch_all($sql, $id = '')
  75. {
  76. $arr = array();
  77. $query = $this->query($sql);
  78. while ($data = $this->fetch_array($query))
  79. {
  80. $id ? $arr[$data[$id]] = $data : $arr[] = $data;
  81. }
  82. return $arr;
  83. }
  84. function cache_gc()
  85. {
  86. $this->query("DELETE FROM {$this->tablepre}sqlcaches WHERE expiry<$this->time");
  87. }
  88. function query($sql, $type = '', $cachetime = FALSE)
  89. {
  90. $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query';
  91. if (!($query = $func($sql, $this->link)) && $type != 'SILENT')
  92. {
  93. $this->halt('MySQL Query Error', $sql);
  94. }
  95. $this->querynum++;
  96. $this->histories[] = $sql;
  97. return $query;
  98. }
  99. function affected_rows()
  100. {
  101. return mysql_affected_rows($this->link);
  102. }
  103. function error()
  104. {
  105. return (($this->link) ? mysql_error($this->link) : mysql_error());
  106. }
  107. function errno()
  108. {
  109. return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
  110. }
  111. function result($query, $row)
  112. {
  113. $query = @mysql_result($query, $row);
  114. return $query;
  115. }
  116. function num_rows($query)
  117. {
  118. $query = mysql_num_rows($query);
  119. return $query;
  120. }
  121. function num_fields($query)
  122. {
  123. return mysql_num_fields($query);
  124. }
  125. function free_result($query)
  126. {
  127. return mysql_free_result($query);
  128. }
  129. function insert_id()
  130. {
  131. return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
  132. }
  133. function fetch_row($query)
  134. {
  135. $query = mysql_fetch_row($query);
  136. return $query;
  137. }
  138. function fetch_fields($query)
  139. {
  140. return mysql_fetch_field($query);
  141. }
  142. function version()
  143. {
  144. return mysql_get_server_info($this->link);
  145. }
  146. function escape_string($str)
  147. {
  148. return mysql_escape_string($str);
  149. }
  150. function close()
  151. {
  152. return mysql_close($this->link);
  153. }
  154. function halt($message = '', $sql = '')
  155. {
  156. $error = mysql_error();
  157. $errorno = mysql_errno();
  158. if ($errorno == 2006 && $this->goneaway-- > 0)
  159. {
  160. $this->connect($this->dbhost, $this->dbuser, $this->dbpw, $this->dbname, $this->dbcharset, $this->pconnect, $this->tablepre, $this->time);
  161. $this->query($sql);
  162. }
  163. else
  164. {
  165. $s = '';
  166. if ($message)
  167. {
  168. $s = "<b>UCenter info:</b> $message<br />";
  169. }
  170. if ($sql)
  171. {
  172. $s .= '<b>SQL:</b>' . htmlspecialchars($sql) . '<br />';
  173. }
  174. $s .= '<b>Error:</b>' . $error . '<br />';
  175. $s .= '<b>Errno:</b>' . $errorno . '<br />';
  176. $s = str_replace(UC_DBTABLEPRE, '[Table]', $s);
  177. exit($s);
  178. }
  179. }
  180. }