dbi.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 980 2009-12-22 03:12:49Z zhaoxiongfei $
  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(!$this->link = new mysqli($dbhost, $dbuser, $dbpw, $dbname)) {
  29. $this->halt('Can not connect to MySQL server');
  30. }
  31. if($this->version() > '4.1') {
  32. if($dbcharset) {
  33. $this->link->set_charset($dbcharset);
  34. }
  35. if($this->version() > '5.0.1') {
  36. $this->link->query("SET sql_mode=''");
  37. }
  38. }
  39. }
  40. function fetch_array($query, $result_type = MYSQLI_ASSOC) {
  41. return $query ? $query->fetch_array($result_type) : null;
  42. }
  43. function result_first($sql) {
  44. $query = $this->query($sql);
  45. return $this->result($query, 0);
  46. }
  47. function fetch_first($sql) {
  48. $query = $this->query($sql);
  49. return $this->fetch_array($query);
  50. }
  51. function fetch_all($sql, $id = '') {
  52. $arr = array();
  53. $query = $this->query($sql);
  54. while($data = $this->fetch_array($query)) {
  55. $id ? $arr[$data[$id]] = $data : $arr[] = $data;
  56. }
  57. return $arr;
  58. }
  59. function cache_gc() {
  60. $this->query("DELETE FROM {$this->tablepre}sqlcaches WHERE expiry<$this->time");
  61. }
  62. function query($sql, $type = '', $cachetime = FALSE) {
  63. $resultmode = $type == 'UNBUFFERED' ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT;
  64. if(!($query = $this->link->query($sql, $resultmode)) && $type != 'SILENT') {
  65. $this->halt('MySQL Query Error', $sql);
  66. }
  67. $this->querynum++;
  68. $this->histories[] = $sql;
  69. return $query;
  70. }
  71. function affected_rows() {
  72. return $this->link->affected_rows;
  73. }
  74. function error() {
  75. return (($this->link) ? $this->link->error : mysqli_error());
  76. }
  77. function errno() {
  78. return intval(($this->link) ? $this->link->errno : mysqli_errno());
  79. }
  80. function result($query, $row) {
  81. if(!$query || $query->num_rows == 0) {
  82. return null;
  83. }
  84. $query->data_seek($row);
  85. $assocs = $query->fetch_row();
  86. return $assocs[0];
  87. }
  88. function num_rows($query) {
  89. $query = $query ? $query->num_rows : 0;
  90. return $query;
  91. }
  92. function num_fields($query) {
  93. return $query ? $query->field_count : 0;
  94. }
  95. function free_result($query) {
  96. return $query ? $query->free() : false;
  97. }
  98. function insert_id() {
  99. return ($id = $this->link->insert_id) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
  100. }
  101. function fetch_row($query) {
  102. $query = $query ? $query->fetch_row() : null;
  103. return $query;
  104. }
  105. function fetch_fields($query) {
  106. return $query ? $query->fetch_field() : null;
  107. }
  108. function version() {
  109. return $this->link->server_info;
  110. }
  111. function escape_string($str) {
  112. return $this->link->escape_string($str);
  113. }
  114. function close() {
  115. return $this->link->close();
  116. }
  117. function halt($message = '', $sql = '') {
  118. $error = $this->error();
  119. $errorno = $this->errno();
  120. if($errorno == 2006 && $this->goneaway-- > 0) {
  121. $this->connect($this->dbhost, $this->dbuser, $this->dbpw, $this->dbname, $this->dbcharset, $this->pconnect, $this->tablepre, $this->time);
  122. $this->query($sql);
  123. } else {
  124. $s = '';
  125. if($message) {
  126. $s = "<b>UCenter info:</b> $message<br />";
  127. }
  128. if($sql) {
  129. $s .= '<b>SQL:</b>'.htmlspecialchars($sql).'<br />';
  130. }
  131. $s .= '<b>Error:</b>'.$error.'<br />';
  132. $s .= '<b>Errno:</b>'.$errorno.'<br />';
  133. $s = str_replace(UC_DBTABLEPRE, '[Table]', $s);
  134. exit($s);
  135. }
  136. }
  137. }
  138. ?>