This repository has been archived by the owner on Feb 13, 2019. It is now read-only.
/
class_mysqldb.php
281 lines (263 loc) · 5.94 KB
/
class_mysqldb.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
279
280
281
<?php
/**
* filename: $Source$
* begin: Friday, Aug 06, 2004
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. This program is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* @author Florian Lippert <flo@redenswert.de>
* @copyright (C) 2003-2004 Florian Lippert
* @package Functions
* @version $Id$
*/
/**
* Class to manage the connection to the Database
* @package Functions
*/
class db
{
/**
* Link ID for every connection
* @var int
*/
var $link_id = 0;
/**
* Query ID for every query
* @var int
*/
var $query_id = 0;
/**
* Errordescription, if an error occures
* @var string
*/
var $errdesc = '';
/**
* Errornumber, if an error occures
* @var int
*/
var $errno = 0;
/**
* Servername
* @var string
*/
var $server = '';
/**
* Username
* @var string
*/
var $user = '';
/**
* Password
* @var string
*/
var $password = '';
/**
* Database
* @var string
*/
var $database = '';
/**
* Class constructor. Connects to Databaseserver and selects Database
*
* @param string Servername
* @param string Username
* @param string Password
* @param string Database
*/
function db($server,$user,$password,$database='')
{
// check for mysql extension
if (!extension_loaded('mysql'))
{
$this->showerror('You should install the PHP MySQL extension!',false);
}
$this->server=$server;
$this->user=$user;
$this->password=$password;
$this->database=$database;
$this->link_id=@mysql_connect($this->server,$this->user,$this->password);
if(!$this->link_id)
{
//try to connect with no password an change it afterwards. only for root user
if($this->user == 'root')
{
$this->link_id=@mysql_connect($this->server,$this->user,'');
if($this->link_id)
{
$this->query("SET PASSWORD = PASSWORD('".$this->escape($this->password)."')");
}
else
{
$this->showerror('Establishing connection failed, exiting');
}
}
else
{
$this->showerror('Establishing connection failed, exiting');
}
}
if($this->database != '')
{
if(!@mysql_select_db($this->database, $this->link_id))
{
$this->showerror('Trying to use database '.$this->database.' failed, exiting');
}
}
}
/**
* Closes connection to Databaseserver
*/
function close()
{
return @mysql_close($this->link_id);
}
/**
* Escapes user input to be used in mysql queries
*
* @param string $input
* @return string escaped string
*/
function escape($input)
{
return mysql_real_escape_string($input, $this->link_id);
}
/**
* Query the Database
*
* @param string Querystring
* @param bool Unbuffered query?
* @return string RessourceId
*/
function query($query_str,$unbuffered=false)
{
global $numbqueries;
if(!$unbuffered)
{
$this->query_id=mysql_query($query_str,$this->link_id);
}
else
{
$this->query_id=mysql_unbuffered_query($query_str,$this->link_id);
}
if(!$this->query_id)
{
$this->showerror('Invalid SQL: '.$query_str);
}
$numbqueries++;
//echo $query_str.' '.$numbqueries.'<br />';
return $this->query_id;
}
/**
* Fetches Row from Query and returns it as array
*
* @param string RessourceId
* @param string Datatype, num or assoc
* @return array The row
*/
function fetch_array($query_id=-1,$datatype='assoc')
{
if($query_id!=-1)
{
$this->query_id=$query_id;
}
if($datatype=='num')
{
$datatype=MYSQL_NUM;
}
else
{
$datatype=MYSQL_ASSOC;
}
$this->record=mysql_fetch_array($this->query_id,$datatype);
return $this->record;
}
/**
* Query Database and fetche the first row from Query and returns it as array
*
* @param string Querystring
* @param string Datatype, num or assoc
* @return array The first row
*/
function query_first($query_string, $datatype='assoc')
{
$this->query($query_string);
return $this->fetch_array($this->query_id,$datatype);
}
/**
* Returns how many rows have been selected
*
* @param string RessourceId
* @return int Number of rows
*/
function num_rows($query_id=-1)
{
if($query_id!=-1)
{
$this->query_id=$query_id;
}
return mysql_num_rows($this->query_id);
}
/**
* Returns the auto_incremental-Value of the inserted row
*
* @return int auto_incremental-Value
*/
function insert_id()
{
return mysql_insert_id($this->link_id);
}
/**
* Returns the number of rows affected by last query
*
* @return int affected rows
*/
function affected_rows()
{
return mysql_affected_rows($this->link_id);
}
/**
* Returns errordescription and errornumber if an error occured.
*
* @return int Errornumber
*/
function geterrdescno()
{
$this->errdesc=mysql_error();
$this->errno=mysql_errno();
return $this->errno;
}
/**
* Dies with an errormessage
*
* @param string Errormessage
*/
function showerror($errormsg,$mysqlActive = true)
{
global $filename;
if($mysqlActive)
{
$this->geterrdescno();
$errormsg .= "\n";
$errormsg .= 'mysql error number: '.$this->errno."\n";
$errormsg .= 'mysql error desc: '.$this->errdesc."\n";
}
$errormsg .= 'Script: '.getenv('REQUEST_URI') ."\n";
$errormsg .= 'Referer: '.getenv('HTTP_REFERER') ."\n";
$errormsg .= 'Time/date: '.date('d/m/Y h:i A') ."\n";
if( (@php_sapi_name() != 'cli') && (@php_sapi_name() != 'cgi') && (@php_sapi_name() != 'cgi-fcgi') )
{
die(nl2br($errormsg));
}
else
{
die($errormsg);
}
}
}
?>