dbi.class.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /*
  3. [UCenter] (C)2001-2009 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: db.class.php 922 2009-02-19 01:30:22Z zhaoxiongfei $
  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 (!$this->link = new mysqli($dbhost, $dbuser, $dbpw, $dbname))
  31. {
  32. $this->halt('Can not connect to MySQL server');
  33. }
  34. if ($this->version() > '4.1')
  35. {
  36. if ($dbcharset)
  37. {
  38. $this->link->set_charset($dbcharset);
  39. }
  40. if ($this->version() > '5.0.1')
  41. {
  42. $this->link->query("SET sql_mode=''");
  43. }
  44. }
  45. }
  46. function fetch_array($query, $result_type = MYSQLI_ASSOC)
  47. {
  48. return $query ? $query->fetch_array($result_type) : null;
  49. }
  50. function result_first($sql)
  51. {
  52. $query = $this->query($sql);
  53. return $this->result($query, 0);
  54. }
  55. function fetch_first($sql)
  56. {
  57. $query = $this->query($sql);
  58. return $this->fetch_array($query);
  59. }
  60. function fetch_all($sql, $id = '')
  61. {
  62. $arr = array();
  63. $query = $this->query($sql);
  64. while ($data = $this->fetch_array($query))
  65. {
  66. $id ? $arr[$data[$id]] = $data : $arr[] = $data;
  67. }
  68. return $arr;
  69. }
  70. function cache_gc()
  71. {
  72. $this->query("DELETE FROM {$this->tablepre}sqlcaches WHERE expiry<$this->time");
  73. }
  74. function query($sql, $type = '', $cachetime = FALSE)
  75. {
  76. $resultmode = $type == 'UNBUFFERED' ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT;
  77. if (!($query = $this->link->query($sql, $resultmode)) && $type != 'SILENT')
  78. {
  79. $this->halt('MySQL Query Error', $sql);
  80. }
  81. $this->querynum++;
  82. $this->histories[] = $sql;
  83. return $query;
  84. }
  85. function affected_rows()
  86. {
  87. return $this->link->affected_rows;
  88. }
  89. function error()
  90. {
  91. return (($this->link) ? $this->link->error : mysqli_error());
  92. }
  93. function errno()
  94. {
  95. return intval(($this->link) ? $this->link->errno : mysqli_errno());
  96. }
  97. function result($query, $row)
  98. {
  99. if (!$query || $query->num_rows == 0)
  100. {
  101. return null;
  102. }
  103. $query->data_seek($row);
  104. $assocs = $query->fetch_row();
  105. return $assocs[0];
  106. }
  107. function num_rows($query)
  108. {
  109. $query = $query ? $query->num_rows : 0;
  110. return $query;
  111. }
  112. function num_fields($query)
  113. {
  114. return $query ? $query->field_count : 0;
  115. }
  116. function free_result($query)
  117. {
  118. return $query ? $query->free() : false;
  119. }
  120. function insert_id()
  121. {
  122. return ($id = $this->link->insert_id) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
  123. }
  124. function fetch_row($query)
  125. {
  126. $query = $query ? $query->fetch_row() : null;
  127. return $query;
  128. }
  129. function fetch_fields($query)
  130. {
  131. return $query ? $query->fetch_field() : null;
  132. }
  133. function version()
  134. {
  135. return $this->link->server_info;
  136. }
  137. function escape_string($str)
  138. {
  139. return $this->link->escape_string($str);
  140. }
  141. function close()
  142. {
  143. return $this->link->close();
  144. }
  145. function halt($message = '', $sql = '')
  146. {
  147. $error = $this->error();
  148. $errorno = $this->errno();
  149. if ($errorno == 2006 && $this->goneaway-- > 0)
  150. {
  151. $this->connect($this->dbhost, $this->dbuser, $this->dbpw, $this->dbname, $this->dbcharset, $this->pconnect, $this->tablepre, $this->time);
  152. $this->query($sql);
  153. }
  154. else
  155. {
  156. $s = '';
  157. if ($message)
  158. {
  159. $s = "<b>UCenter info:</b> $message<br />";
  160. }
  161. if ($sql)
  162. {
  163. $s .= '<b>SQL:</b>' . htmlspecialchars($sql) . '<br />';
  164. }
  165. $s .= '<b>Error:</b>' . $error . '<br />';
  166. $s .= '<b>Errno:</b>' . $errorno . '<br />';
  167. $s = str_replace(UC_DBTABLEPRE, '[Table]', $s);
  168. exit($s);
  169. }
  170. }
  171. }