db.class.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /*
  3. [Discuz!] (C)2001-2099 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: db.class.php 1152 2013-05-28 09:11:04Z kamichen $
  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($pconnect) {
  20. if(!$this->link = mysql_pconnect($dbhost, $dbuser, $dbpw)) {
  21. $this->halt('Can not connect to MySQL server');
  22. }
  23. } else {
  24. if(!$this->link = mysql_connect($dbhost, $dbuser, $dbpw, 1)) {
  25. $this->halt('Can not connect to MySQL server');
  26. }
  27. }
  28. if($this->version() > '4.1') {
  29. if($dbcharset) {
  30. mysql_query("SET character_set_connection=".$dbcharset.", character_set_results=".$dbcharset.", character_set_client=binary", $this->link);
  31. }
  32. if($this->version() > '5.0.1') {
  33. mysql_query("SET sql_mode=''", $this->link);
  34. }
  35. }
  36. if($dbname) {
  37. mysql_select_db($dbname, $this->link);
  38. }
  39. }
  40. function fetch_array($query, $result_type = MYSQL_ASSOC) {
  41. return mysql_fetch_array($query, $result_type);
  42. }
  43. function result_first($sql, &$data) {
  44. $query = $this->query($sql);
  45. $data = $this->result($query, 0);
  46. }
  47. function fetch_first($sql, &$arr) {
  48. $query = $this->query($sql);
  49. $arr = $this->fetch_array($query);
  50. }
  51. function fetch_all($sql, &$arr) {
  52. $query = $this->query($sql);
  53. while($data = $this->fetch_array($query)) {
  54. $arr[] = $data;
  55. }
  56. }
  57. function cache_gc() {
  58. $this->query("DELETE FROM {$this->tablepre}sqlcaches WHERE expiry<$this->time");
  59. }
  60. function query($sql, $type = '', $cachetime = FALSE) {
  61. $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query';
  62. if(!($query = $func($sql, $this->link)) && $type != 'SILENT') {
  63. $this->halt('MySQL Query Error', $sql);
  64. }
  65. $this->querynum++;
  66. $this->histories[] = $sql;
  67. return $query;
  68. }
  69. function affected_rows() {
  70. return mysql_affected_rows($this->link);
  71. }
  72. function error() {
  73. return (($this->link) ? mysql_error($this->link) : mysql_error());
  74. }
  75. function errno() {
  76. return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
  77. }
  78. function result($query, $row) {
  79. $query = @mysql_result($query, $row);
  80. return $query;
  81. }
  82. function num_rows($query) {
  83. $query = mysql_num_rows($query);
  84. return $query;
  85. }
  86. function num_fields($query) {
  87. return mysql_num_fields($query);
  88. }
  89. function free_result($query) {
  90. return mysql_free_result($query);
  91. }
  92. function insert_id() {
  93. return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
  94. }
  95. function fetch_row($query) {
  96. $query = mysql_fetch_row($query);
  97. return $query;
  98. }
  99. function fetch_fields($query) {
  100. return mysql_fetch_field($query);
  101. }
  102. function version() {
  103. return mysql_get_server_info($this->link);
  104. }
  105. function escape_string($str) {
  106. return mysql_escape_string($str);
  107. }
  108. function close() {
  109. return mysql_close($this->link);
  110. }
  111. function halt($message = '', $sql = '') {
  112. show_error('run_sql_error', $message.'<br /><br />'.$sql.'<br /> '.mysql_error(), 0);
  113. }
  114. }
  115. ?>