/
dbwrapper_mysqli_oos.php
216 lines (189 loc) · 5.41 KB
/
dbwrapper_mysqli_oos.php
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
// addnews ready
// translator ready
// mail ready
function db_query($sql, $die=true){
if (defined("DB_NODB") && !defined("LINK")) return array();
global $session,$dbinfo,$mysqli_resource;
$dbinfo['queriesthishit']++;
// $fname = DBTYPE."_query";
$starttime = getmicrotime();
//$r = $fname($sql);
$r = $mysqli_resource->Query($sql);
if (!$r && $die === true) {
if (defined("IS_INSTALLER") && IS_INSTALLER){
return array();
}else{
if ($session['user']['superuser'] & SU_DEVELOPER){
require_once("lib/show_backtrace.php");
die(
"<pre>".HTMLEntities($sql, ENT_COMPAT, getsetting("charset", "ISO-8859-1"))."</pre>"
.db_error(LINK)
.show_backtrace()
);
}else{
die("A most bogus error has occurred. I apologise, but the page you were trying to access is broken. Please use your browser's back button and try again.");
}
}
}
$endtime = getmicrotime();
if ($endtime - $starttime >= 1.00 && ($session['user']['superuser'] & SU_DEBUG_OUTPUT)){
$s = trim($sql);
if (strlen($s) > 800) $s = substr($s,0,400)." ... ".substr($s,strlen($s)-400);
debug("Slow Query (".round($endtime-$starttime,2)."s): ".(HTMLEntities($s, ENT_COMPAT, getsetting("charset", "ISO-8859-1")))."`n");
}
unset($dbinfo['affected_rows']);
$dbinfo['affected_rows']=db_affected_rows();
$dbinfo['querytime'] += $endtime-$starttime;
return $r;
}
//& at the start returns a reference to the data array.
//since it's possible this array is large, we'll save ourselves
//the overhead of duplicating the array, then destroying the old
//one by returning a reference instead.
function &db_query_cached($sql,$name,$duration=900){
//this function takes advantage of the data caching library to make
//all of the other db_functions act just like MySQL queries but rely
//instead on disk cached data.
//if (getsetting("usedatacache", 0) == 1) debug("DataCache: $name");
//standard is 15 minutes, als hooks don't need to be cached *that* often, normally you invalidate the cache properly
global $dbinfo;
$data = datacache($name,$duration);
if (is_array($data)){
reset($data);
$dbinfo['affected_rows']=-1;
return $data;
}else{
$result = db_query($sql);
$data = array();
while ($row = db_fetch_assoc($result)) {
$data[] = $row;
}
updatedatacache($name,$data);
reset($data);
return $data;
}
}
if (file_exists("lib/dbremote.php")) {
require_once("lib/dbremote.php");
}
function db_error(){
global $mysqli_resource;
$r = $mysqli_resource->error;
if ($r=="" && defined("DB_NODB") && !defined("DB_INSTALLER_STAGE4")) return "The database connection was never established";
return $r;
}
function db_fetch_assoc(&$result){
if (is_array($result)){
//cached data
if (list($key,$val)=each($result))
return $val;
else
return false;
}else{
//$fname = DBTYPE."_fetch_assoc";
//$r = $fname($result);
$r = $result->Fetch_Assoc();
return $r;
}
}
function db_insert_id(){
global $mysqli_resource;
if (defined("DB_NODB") && !defined("LINK")) return -1;
//$fname = DBTYPE."_insert_id";
//$r = $fname();
$r = $mysqli_resource->insert_id;
return $r;
}
function db_num_rows($result){
if (is_array($result)){
return count($result);
}else{
if (defined("DB_NODB") && !defined("LINK")) return 0;
$r = $result->num_rows;
return $r;
}
}
function db_affected_rows($link=false){
global $dbinfo, $mysqli_resource;
if (isset($dbinfo['affected_rows'])) {
return $dbinfo['affected_rows'];
}
if (defined("DB_NODB") && !defined("LINK")) return 0;
$r = $mysqli_resource->affected_rows;
return $r;
}
function db_pconnect($host,$user,$pass){
global $mysqli_resource;
// Constants cannot be an object
// MySQLi do not know a pconnect
$mysqli_resource = New MySQLi($host, $user, $pass);
if($mysqli_resource) {
return true;
}
else {
return false;
}
}
function db_connect($host,$user,$pass){
global $mysqli_resource;
// Constants cannot be an object
$mysqli_resource = New MySQLi($host, $user, $pass);
if($mysqli_resource) {
return true;
}
else {
return false;
}
}
function db_get_server_version() {
global $mysqli_resource;
return $mysqli_resource->server_info;
}
function db_select_db($dbname){
global $mysqli_resource;
$r = $mysqli_resource->select_db($dbname);
return $r;
}
function db_real_escape_string($string){
global $mysqli_resource;
$r = $mysqli_resource->real_escape_string($string);
return $r;
}
function db_free_result($result){
if (is_array($result)){
//cached data
unset($result);
}else{
if (defined("DB_NODB") && !defined("LINK")) return false;
$result->Free();
return true;
}
}
function db_table_exists($tablename){
global $mysqli_resource;
if (defined("DB_NODB") && !defined("LINK")) return false;
$exists = $mysqli_resource->Query("SELECT 1 FROM `$tablename` LIMIT 0");
if ($exists) return true;
return false;
}
function db_prefix($tablename, $force=false) {
global $DB_PREFIX;
if ($force === false) {
$special_prefixes = array();
// The following file should be used to override or modify the
// special_prefixes array to be correct for your site. Do NOT
// do this unles you know EXACTLY what this means to you, your
// game, your county, your state, your nation, your planet and
// your universe!
if (file_exists("prefixes.php")) require_once("prefixes.php");
$prefix = $DB_PREFIX;
if (isset($special_prefixes[$tablename])) {
$prefix = $special_prefixes[$tablename];
}
} else {
$prefix = $force;
}
return $prefix . $tablename;
}
?>