db.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 1059 2011-03-01 07:25:09Z monkey $
  6. */
  7. class ucclient_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 close() {
  117. return mysql_close($this->link);
  118. }
  119. function halt($message = '', $sql = '') {
  120. $error = mysql_error();
  121. $errorno = mysql_errno();
  122. if($errorno == 2006 && $this->goneaway-- > 0) {
  123. $this->connect($this->dbhost, $this->dbuser, $this->dbpw, $this->dbname, $this->dbcharset, $this->pconnect, $this->tablepre, $this->time);
  124. $this->query($sql);
  125. } else {
  126. $s = '';
  127. if($message) {
  128. $s = "<b>UCenter info:</b> $message<br />";
  129. }
  130. if($sql) {
  131. $s .= '<b>SQL:</b>'.htmlspecialchars($sql).'<br />';
  132. }
  133. $s .= '<b>Error:</b>'.$error.'<br />';
  134. $s .= '<b>Errno:</b>'.$errorno.'<br />';
  135. $s = str_replace(UC_DBTABLEPRE, '[Table]', $s);
  136. exit($s);
  137. }
  138. }
  139. }
  140. ?>