dbi.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /*
  3. [Discuz!] (C)2001-2009 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: db.class.php 976 2009-12-03 03:02:44Z zhaoxiongfei $
  6. */
  7. if(!defined('IN_COMSENZ')) {
  8. exit('Access Denied');
  9. }
  10. class dbstuff {
  11. var $querynum = 0;
  12. var $link;
  13. var $histories;
  14. var $time;
  15. var $tablepre;
  16. function connect($dbhost, $dbuser, $dbpw, $dbname = '', $dbcharset, $pconnect = 0, $tablepre='', $time = 0) {
  17. $this->time = $time;
  18. $this->tablepre = $tablepre;
  19. if(!$this->link = new mysqli($dbhost, $dbuser, $dbpw, $dbname)) {
  20. $this->halt('Can not connect to MySQL server');
  21. }
  22. if($this->version() > '4.1') {
  23. if($dbcharset) {
  24. $this->link->set_charset($dbcharset);
  25. }
  26. if($this->version() > '5.0.1') {
  27. $this->link->query("SET sql_mode=''");
  28. }
  29. }
  30. }
  31. function fetch_array($query, $result_type = MYSQLI_ASSOC) {
  32. return $query ? $query->fetch_array($result_type) : null;
  33. }
  34. function result_first($sql, &$data) {
  35. $query = $this->query($sql);
  36. $data = $this->result($query, 0);
  37. }
  38. function fetch_first($sql, &$arr) {
  39. $query = $this->query($sql);
  40. $arr = $this->fetch_array($query);
  41. }
  42. function fetch_all($sql, &$arr) {
  43. $query = $this->query($sql);
  44. while($data = $this->fetch_array($query)) {
  45. $arr[] = $data;
  46. }
  47. }
  48. function cache_gc() {
  49. $this->query("DELETE FROM {$this->tablepre}sqlcaches WHERE expiry<$this->time");
  50. }
  51. function query($sql, $type = '', $cachetime = FALSE) {
  52. $resultmode = $type == 'UNBUFFERED' ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT;
  53. if(!($query = $this->link->query($sql, $resultmode)) && $type != 'SILENT') {
  54. $this->halt('MySQL Query Error', $sql);
  55. }
  56. $this->querynum++;
  57. $this->histories[] = $sql;
  58. return $query;
  59. }
  60. function affected_rows() {
  61. return $this->link->affected_rows;
  62. }
  63. function error() {
  64. return (($this->link) ? $this->link->error : mysqli_error());
  65. }
  66. function errno() {
  67. return intval(($this->link) ? $this->link->errno : mysqli_errno());
  68. }
  69. function result($query, $row) {
  70. if(!$query || $query->num_rows == 0) {
  71. return null;
  72. }
  73. $query->data_seek($row);
  74. $assocs = $query->fetch_row();
  75. return $assocs[0];
  76. }
  77. function num_rows($query) {
  78. $query = $query ? $query->num_rows : 0;
  79. return $query;
  80. }
  81. function num_fields($query) {
  82. return $query ? $query->field_count : 0;
  83. }
  84. function free_result($query) {
  85. return $query ? $query->free() : false;
  86. }
  87. function insert_id() {
  88. return ($id = $this->link->insert_id) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
  89. }
  90. function fetch_row($query) {
  91. $query = $query ? $query->fetch_row() : null;
  92. return $query;
  93. }
  94. function fetch_fields($query) {
  95. return $query ? $query->fetch_field() : null;
  96. }
  97. function version() {
  98. return $this->link->server_info;
  99. }
  100. function escape_string($str) {
  101. return $this->link->escape_string($str);
  102. }
  103. function close() {
  104. return $this->link->close();
  105. }
  106. function halt($message = '', $sql = '') {
  107. show_error('run_sql_error', $message.'<br /><br />'.$sql.'<br /> '.$this->error(), 0);
  108. }
  109. }
  110. ?>