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