-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
278 lines (243 loc) · 6.77 KB
/
db.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/*
You can implement a new db driver in this file by uncommenting the line below
and replacing 'mssql' with the desired driver name, then adding cases for the
that driver in each function below ...
*/
define('DATABASE', 'sqlserver');
if(!defined('DATABASE')){
if(function_exists('mysqli_connect')){
define('DATABASE', 'mysqli');
}else{
define('DATABASE', 'mysql');
}
}
//define('mysql_charset', 'ascii');
function db_link($link = NULL){
static $db_link;
if($link) $db_link = $link;
return $db_link;
}
function db_close($link = NULL){
db_link(false); /* close db link */
switch(DATABASE){
case 'mysql':
return mysql_close($link);
case 'mysqli':
return mysqli_close($link);
case 'sqlserver':
odbc_close($link);
return false;
}
}
function db_select_db($dbname, $link = NULL){
if(!$link) $link = db_link();
switch(DATABASE){
case 'mysql':
return mysql_select_db($dbname, $link);
case 'mysqli':
return mysqli_select_db($link, $dbname);
case 'sqlserver':
return true;
}
}
function db_fetch_array($res){
switch(DATABASE){
case 'mysql':
return mysql_fetch_array($res);
case 'mysqli':
return mysqli_fetch_array($res, MYSQLI_BOTH);
case 'sqlserver':
return array_values(db_fetch_assoc($res));
}
}
function db_fetch_assoc($res){
switch(DATABASE){
case 'mysql':
return mysql_fetch_assoc($res);
case 'mysqli':
return mysqli_fetch_assoc($res);
case 'sqlserver':
return odbc_fetch_array($res);
}
}
function db_fetch_row($res){
switch(DATABASE){
case 'mysql':
return mysql_fetch_row($res);
case 'mysqli':
return mysqli_fetch_row($res);
case 'sqlserver':
return odbc_fetch_row($res) ;
}
}
function db_num_fields($res){
switch(DATABASE){
case 'mysql':
return mysql_num_fields($res);
case 'mysqli':
return mysqli_num_fields($res);
case 'sqlserver':
return odbc_num_fields($res);
}
}
function db_num_rows($res){
switch(DATABASE){
case 'mysql':
return mysql_num_rows($res);
case 'mysqli':
return mysqli_num_rows($res);
case 'sqlserver':
return odbc_num_rows($res);
}
}
function db_affected_rows($link = NULL){
if(!$link) $link = db_link();
switch(DATABASE){
case 'mysql':
return mysql_affected_rows($link);
case 'mysqli':
return mysqli_affected_rows($link);
case 'sqlserver':
return odbc_num_rows($result);
}
}
function db_query($query, $link = NULL){
if(!$link) $link = db_link();
switch(DATABASE){
case 'mysql':
return mysql_query($query, $link);
case 'mysqli':
return mysqli_query($link, $query);
case 'sqlserver':
$result=odbc_exec($link,$query);
return $result;
}
}
function db_insert_id($link = NULL){
if(!$link) $link = db_link();
switch(DATABASE){
case 'mysql':
return mysql_insert_id($link);
case 'mysqli':
return mysqli_insert_id($link);
case 'sqlserver':
$query="SELECT @@IDENTITY";
$res=db_query($query, NULL);
$data=db_fetch_array($res);
return $data[0];
}
}
function db_field_name($res, $field_offset){
switch(DATABASE){
case 'mysql':
return mysql_field_name($res, $field_offset);
case 'mysqli':
$fo = mysqli_fetch_field_direct($res, $field_offset);
if($fo === false) return false;
return $fo->name;
case 'sqlserver':
return odbc_field_name($res,$field_offset);
}
}
function db_field_type($res, $field_offset){
switch(DATABASE){
case 'mysql':
return mysql_field_type($res, $field_offset);
case 'mysqli':
$fo = mysqli_fetch_field_direct($res, $field_offset);
if($fo === false) return false;
return $fo->type;
case 'sqlserver':
return odbc_field_type($res, $field_offset);
}
}
function db_escape($str = NULL, $link = NULL){
if(!$link) $link = db_link();
switch(DATABASE){
case 'mysql':
if(function_exists('mysql_real_escape_string')) return mysql_real_escape_string($str, $link);
return mysql_escape_string($str);
case 'mysqli':
return mysqli_real_escape_string($link, $str);
case 'sqlserver':
if ( !isset($str) or empty($str) ) return '';
if ( is_numeric($str) ){
return $str;
}
else{
return (str_replace( '"', "'", $str ));
}
}
}
function db_connect($host = NULL, $username = NULL, $passwd = NULL, $dbname = NULL, $port = NULL, $socket = NULL){
switch(DATABASE){
case 'mysql':
if($host === NULL) $host = ini_get("mysql.default_host");
if($username === NULL) $username = ini_get("mysql.default_user");
if($passwd === NULL) $passwd = ini_get("mysql.default_password");
$link = mysql_connect($host, $username, $passwd);
if(!$link) return false;
db_link($link); /* db_link() can now be used to retrieve the db link from anywhere */
@mysql_query("SET NAMES '" . mysql_charset . "'");
if($dbname){ if(!mysql_select_db($dbname, $link)) return false; }
return $link;
case 'mysqli':
if($host === NULL) $host = ini_get("mysqli.default_host");
if($username === NULL) $username = ini_get("mysqli.default_user");
if($passwd === NULL) $passwd = ini_get("mysqli.default_pw");
if($dbname === NULL) $dbname = "";
if($port === NULL) $port = ini_get("mysqli.default_port");
if($socket === NULL) $socket = ini_get("mysqli.default_socket");
$link = mysqli_connect($host, $username, $passwd, $dbname, $port, $socket);
if(!$link) return false;
db_link($link); /* db_link() can now be used to retrieve the db link from anywhere */
mysqli_set_charset($link, mysql_charset);
return $link;
case 'sqlserver':
if($host === NULL) $host ='';
if($username === NULL) $username = "";
if($passwd === NULL) $passwd ="";
if($dbname === NULL)
{
$dbname = "";
$connection_string = "DRIVER={SQL Server};SERVER=$host;";
}
else{
$connection_string = "DRIVER={SQL Server};SERVER=$host;DATABASE=$dbname";
}
$link = odbc_connect($connection_string,$username,$passwd);
if(!$link)
{
return false;
}
else{
db_link($link);
return $link;
}
}
}
function db_errno($link = NULL, $mysqli_connect = false){
if(!$link) $link = db_link();
switch(DATABASE){
case 'mysql':
return mysql_errno($link);
case 'mysqli':
if($mysqli_connect) return mysqli_connect_errno();
return mysqli_errno($link);
case 'sqlserver':
return odbc_error();
}
}
function db_error($link = NULL, $mysqli_connect = false){
if(!$link) $link = db_link();
switch(DATABASE){
case 'mysql':
return mysql_error($link);
case 'mysqli':
if($mysqli_connect) return mysqli_connect_error();
return mysqli_error($link);
case 'sqlserver':
return odbc_errormsg($link);
}
}