Heya one2xs,
Ik ben een beetje OOP aan het bestuderen en heb een database class geschreven. Ik heb enkel de vraag of ik nog functies mis:
class database
Bovendien slaat hij het aantal gebruikte queries, de tijd van elk van die queries, de queries zelf, en de totale tijd op. Voor debugging.
Je mag hem gebruiken, als je hem maar niet doorverkoopt, doorgeeft of wat dan ook onder eigen naam. Ik ben er al achter gekomen dat enkele van mijn vorige betaalde, danwel gratis scripts onder eigen naam werden doorverkocht, en als dit nuw eer het geval wordt dan is het uit met de pret en zal ik enkel nog betaald scripten. En dit keer echt geen uitzonderingen, je houd je maar gewoon aan de copyright & licentie!
Donaties zijn welkom, indien je de class gebruikt + handig vindt.
Codeblok (dbclass.inc.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/**
* @package Database class
*
* @version 0.9.0
* @author Rik S. [http://www.MyWerewolf.nl / http://www.Just-rik.nl]
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice and this (list of) condition(s).
* @copyright Rik S.
*/
class DataBase
{
public $QueryTimer;
public $QueryCount;
public $QueryTimeArray = array();
public $QueryQueryArray = array();
public function __construct($DbHost = NULL, $DbUsername = NULL, $DbPassword = NULL, $DbDatabase = NULL)
{
if($DbHost != NULL && $DbUsername != NULL && $DbPassword != NULL && $DbDatabase != NULL)
{
$this->connect($DbHost, $DbUsername, $DbPassword, $DbDatabase);
}
}
public function connect($DbHost = NULL, $DbUsername = NULL, $DbPassword = NULL, $DbDatabase = NULL)
{
try
{
@$DbConnect = mysql_connect($DbHost, $DbUsername, $DbPassword);
if($DbConnect == FALSE)
{
$ErrorStr = No connection to database;
$ErrorStr .=
Host: . $DbHost .
;
$ErrorStr .= Username: . $DbUsername .
;
throw new Exception($ErrorStr);
}
else
{
if(@mysql_select_db($DbDatabase, $DbConnect) == FALSE)
{
$ErrorStr = Could not select Database;
$ErrorStr .=
Host: . $DbHost .
;
$ErrorStr .= Username: . $DbUsername .
;
$ErrorStr .= Database: . $DbDatabase;
throw new Exception($ErrorStr);
}
else
{
return TRUE;
}
}
}
catch(Exception $e)
{
$this->error($e);
}
}
public function query($input = NULL)
{
try
{
if($input != NULL)
{
$TimeStart = microtime();
$sQuery = mysql_query($input);
if($sQuery !== FALSE)
{
$TimeTotal = microtime() - $TimeStart;
$this->QueryTimer += $TimeTotal;
$this->QueryTimeArray[] = $TimeTotal;
$this->QueryQueryArray[] = $input;
$this->QueryCount++;
return $sQuery;
}
else
{
$ErrorString = Query failed to execute
;
$ErrorString .= mysql_error() .
;
$ErrorString .= Query: . $input;
throw new Exception($ErrorString);
}
}
else
{
throw new Exception(Query is empty);
}
}
catch(Exception $e)
{
$this->error($e);
}
}
public function num_rows($resource = NULL)
{
try
{
if($resource != NULL)
{
if(is_resource($resource))
{
return mysql_num_rows($resource);
}
else
{
throw new Exception(Input is not a resource);
}
}
else
{
throw new Exception(Empty resource);
}
}
catch(Exception $e)
{
$this->error($e);
}
}
public function fetch($resource = NULL, $type = assoc)
{
try
{
if($resource != NULL)
{
if(is_resource($resource))
{
if($type == assoc)
{
return mysql_fetch_assoc($resource);
}
elseif($type == array)
{
return mysql_fetch_array($resource);
}
elseif($type == object)
{
return mysql_fetch_object($resource);
}
else
{
throw new Exception(Invalid type
Accepted values: assoc, array, object);
}
}
else
{
throw new Exception(Input is not a resource);
}
}
else
{
throw new Exception(Empty resource);
}
}
catch(Exception $e)
{
$this->error($e);
}
}
public function data_seek ($resource = NULL, $row = NULL)
{
try{
if($resource != NULL)
{
if($row == NULL)
{
return mysql_data_seek($resource, 0);
}
elseif(is_numeric($row))
{
if($row >= $this->num_rows($resource) || $row < 0)
{
throw new Exception(Row number is outside the range of 0 - . $this->num_rows($resource));
}
else
{
return mysql_data_seek($resource, $row);
}
}
else
{
throw new Exception(Invalid row, row is not a number);
}
}
else
{
throw new Exception(Empty resource);
}
}
catch(Exception $e)
{
$this->error($e);
}
}
public function affected($resource = NULL)
{
try
{
if(@mysql_affected_rows($resource) == FALSE)
{
throw new Exception(No query found with update or delete statements);
}
else
{
return mysql_affected_rows($resource);
}
}
catch(Exception $e)
{
$this->error($e);
}
}
public function insert_id($resource = NULL)
{
if($resource != NULL)
{
return mysql_insert_id($resource);
}
else
{
return mysql_insert_id();
}
}
public function escape($input = NULL)
{
try
{
if($input != NULL)
{
return mysql_real_escape_string($input);
}
else
{
throw new Exception(Empty string);
}
}
catch(Exception $e)
{
$this->error($e);
}
}
public function querycount()
{
return $this->QueryCount == 0 ? no : $this->QueryCount;
}
public function querytime()
{
return $this->QueryTimer == 0 ? no : $this->QueryTimer;
}
public function querytime_array($input = NULL)
{
try
{
if(count($this->QueryQueryArray) != 0)
{
if($input == NULL)
{
$QueryString = ;
for($i = 0; $i < count($this->QueryQueryArray); $i++)
{
$QueryString .= $this->QueryQueryArray[$i] . took . $this->QueryTimeArray[$i] . seconds
;
}
return $QueryString;
}
else
{
if(is_numeric($input))
{
if(isset($this->QueryQueryArray[$input]))
{
return $this->QueryQueryArray[$input] . took . $this->QueryTimeArray[$input] . seconds
;
}
else
{
throw new Exception(Query # . $input . does not exist);
}
}
else
{
throw new Exception(Invalid input, only numbers allowed);
}
}
}
else
{
return No querys executed;
}
}
catch(Exception $e)
{
$this->error($e);
}
}
private function error($e)
{
if (is_object($e))
{
$v = sprintf(Line: %s
File:
%s
Request-URI
%s
Error:
%s, $e->getLine(), $e->getFile(), $_SERVER[REQUEST_URI], $e->getMessage());
}
elseif (is_string($e)) {
$v = $e;
}
echo sprintf(
, $v);
exit;
}
}
?>
Ik ben een beetje OOP aan het bestuderen en heb een database class geschreven. Ik heb enkel de vraag of ik nog functies mis:
class database
- connect() - Connect naar de in de class gedefineerde host + database
- query($input) - Probeerd de sql statements in $input uit te voeren.
- num_rows($resource) - Geeft aantal rows van resource terug
- fetch($resource, $type) - Fetched de resource in het type $type [object, assoc, array]
- escape($input) - Escaped de input
- affected($resource) - Haalt affected rows op van $resource, of van laatste connectie waneer resource leeg is.
- insert_id($resource) - Haalt auto_increment waarde op van $resource, of van laatste connectie waneer resource leeg is.
- data_seek ($resource, $row) - (kan ik niet in nederlands uitleggen, dus van php.net
Moves the internal row pointer of the MySQL result associated with the specified result identifier to point to the specified row number - querytime() - Geeft de totale tijd van qeuries
- querycount() - Geeft het aantal uitgevoerde queries weer
- querytime_array($input) - Geeft query + tijd van query met het nummer in $input, of de hele lijst waneer $input leeg is.
- error($e) - Laat eventuele errors die opgevangen zijn zien [ http://just-rik.nl/test/DbClass.php ]
Bovendien slaat hij het aantal gebruikte queries, de tijd van elk van die queries, de queries zelf, en de totale tijd op. Voor debugging.
Je mag hem gebruiken, als je hem maar niet doorverkoopt, doorgeeft of wat dan ook onder eigen naam. Ik ben er al achter gekomen dat enkele van mijn vorige betaalde, danwel gratis scripts onder eigen naam werden doorverkocht, en als dit nuw eer het geval wordt dan is het uit met de pret en zal ik enkel nog betaald scripten. En dit keer echt geen uitzonderingen, je houd je maar gewoon aan de copyright & licentie!
Donaties zijn welkom, indien je de class gebruikt + handig vindt.
Codeblok (dbclass.inc.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/**
* @package Database class
*
* @version 0.9.0
* @author Rik S. [http://www.MyWerewolf.nl / http://www.Just-rik.nl]
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice and this (list of) condition(s).
* @copyright Rik S.
*/
class DataBase
{
public $QueryTimer;
public $QueryCount;
public $QueryTimeArray = array();
public $QueryQueryArray = array();
public function __construct($DbHost = NULL, $DbUsername = NULL, $DbPassword = NULL, $DbDatabase = NULL)
{
if($DbHost != NULL && $DbUsername != NULL && $DbPassword != NULL && $DbDatabase != NULL)
{
$this->connect($DbHost, $DbUsername, $DbPassword, $DbDatabase);
}
}
public function connect($DbHost = NULL, $DbUsername = NULL, $DbPassword = NULL, $DbDatabase = NULL)
{
try
{
@$DbConnect = mysql_connect($DbHost, $DbUsername, $DbPassword);
if($DbConnect == FALSE)
{
$ErrorStr = No connection to database;
$ErrorStr .=
Host: . $DbHost .
;
$ErrorStr .= Username: . $DbUsername .
;
throw new Exception($ErrorStr);
}
else
{
if(@mysql_select_db($DbDatabase, $DbConnect) == FALSE)
{
$ErrorStr = Could not select Database;
$ErrorStr .=
Host: . $DbHost .
;
$ErrorStr .= Username: . $DbUsername .
;
$ErrorStr .= Database: . $DbDatabase;
throw new Exception($ErrorStr);
}
else
{
return TRUE;
}
}
}
catch(Exception $e)
{
$this->error($e);
}
}
public function query($input = NULL)
{
try
{
if($input != NULL)
{
$TimeStart = microtime();
$sQuery = mysql_query($input);
if($sQuery !== FALSE)
{
$TimeTotal = microtime() - $TimeStart;
$this->QueryTimer += $TimeTotal;
$this->QueryTimeArray[] = $TimeTotal;
$this->QueryQueryArray[] = $input;
$this->QueryCount++;
return $sQuery;
}
else
{
$ErrorString = Query failed to execute
;
$ErrorString .= mysql_error() .
;
$ErrorString .= Query: . $input;
throw new Exception($ErrorString);
}
}
else
{
throw new Exception(Query is empty);
}
}
catch(Exception $e)
{
$this->error($e);
}
}
public function num_rows($resource = NULL)
{
try
{
if($resource != NULL)
{
if(is_resource($resource))
{
return mysql_num_rows($resource);
}
else
{
throw new Exception(Input is not a resource);
}
}
else
{
throw new Exception(Empty resource);
}
}
catch(Exception $e)
{
$this->error($e);
}
}
public function fetch($resource = NULL, $type = assoc)
{
try
{
if($resource != NULL)
{
if(is_resource($resource))
{
if($type == assoc)
{
return mysql_fetch_assoc($resource);
}
elseif($type == array)
{
return mysql_fetch_array($resource);
}
elseif($type == object)
{
return mysql_fetch_object($resource);
}
else
{
throw new Exception(Invalid type
Accepted values: assoc, array, object);
}
}
else
{
throw new Exception(Input is not a resource);
}
}
else
{
throw new Exception(Empty resource);
}
}
catch(Exception $e)
{
$this->error($e);
}
}
public function data_seek ($resource = NULL, $row = NULL)
{
try{
if($resource != NULL)
{
if($row == NULL)
{
return mysql_data_seek($resource, 0);
}
elseif(is_numeric($row))
{
if($row >= $this->num_rows($resource) || $row < 0)
{
throw new Exception(Row number is outside the range of 0 - . $this->num_rows($resource));
}
else
{
return mysql_data_seek($resource, $row);
}
}
else
{
throw new Exception(Invalid row, row is not a number);
}
}
else
{
throw new Exception(Empty resource);
}
}
catch(Exception $e)
{
$this->error($e);
}
}
public function affected($resource = NULL)
{
try
{
if(@mysql_affected_rows($resource) == FALSE)
{
throw new Exception(No query found with update or delete statements);
}
else
{
return mysql_affected_rows($resource);
}
}
catch(Exception $e)
{
$this->error($e);
}
}
public function insert_id($resource = NULL)
{
if($resource != NULL)
{
return mysql_insert_id($resource);
}
else
{
return mysql_insert_id();
}
}
public function escape($input = NULL)
{
try
{
if($input != NULL)
{
return mysql_real_escape_string($input);
}
else
{
throw new Exception(Empty string);
}
}
catch(Exception $e)
{
$this->error($e);
}
}
public function querycount()
{
return $this->QueryCount == 0 ? no : $this->QueryCount;
}
public function querytime()
{
return $this->QueryTimer == 0 ? no : $this->QueryTimer;
}
public function querytime_array($input = NULL)
{
try
{
if(count($this->QueryQueryArray) != 0)
{
if($input == NULL)
{
$QueryString = ;
for($i = 0; $i < count($this->QueryQueryArray); $i++)
{
$QueryString .= $this->QueryQueryArray[$i] . took . $this->QueryTimeArray[$i] . seconds
;
}
return $QueryString;
}
else
{
if(is_numeric($input))
{
if(isset($this->QueryQueryArray[$input]))
{
return $this->QueryQueryArray[$input] . took . $this->QueryTimeArray[$input] . seconds
;
}
else
{
throw new Exception(Query # . $input . does not exist);
}
}
else
{
throw new Exception(Invalid input, only numbers allowed);
}
}
}
else
{
return No querys executed;
}
}
catch(Exception $e)
{
$this->error($e);
}
}
private function error($e)
{
if (is_object($e))
{
$v = sprintf(Line: %s
File:
%s
Request-URI
%s
Error:
%s, $e->getLine(), $e->getFile(), $_SERVER[REQUEST_URI], $e->getMessage());
}
elseif (is_string($e)) {
$v = $e;
}
echo sprintf(
%s
, $v);
exit;
}
}
?>
[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/include"]include[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]dbclass.inc.php[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #0000BB]$MySQL[/COLOR] [COLOR= #007700]= new[/COLOR] DataBase[COLOR= #007700]();[/COLOR]
[COLOR= #FF8000]// Connecten, interne error handler stopt het script wanneer een error is gevonden, dus hoeven wij hier niet naar te kijken![/COLOR]
[COLOR= #0000BB]$MySQL[/COLOR][COLOR= #007700]->[/COLOR]connect[COLOR= #007700]();[/COLOR]
[COLOR= #FF8000]// Query =D[/COLOR]
[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]QUERY: SELECT uName FROM member_accounts WHERE uId = 1 || uId = 2<br />[/COLOR][COLOR= #007700];[/COLOR]
[COLOR= #0000BB]$Query[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$MySQL[/COLOR][COLOR= #007700]->[/COLOR]query[COLOR= #007700]([/COLOR][COLOR= #DD0000]"SELECT uName FROM member_accounts WHERE uId = 1 || uId = 2"[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #FF8000]// Num rows[/COLOR]
[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]$MySQL->num_rows: [/COLOR] [COLOR= #007700].[/COLOR] [COLOR= #0000BB]$MySQL[/COLOR][COLOR= #007700]->[/COLOR]num_rows[COLOR= #007700]([/COLOR] [COLOR= #0000BB]$Query[/COLOR] [COLOR= #007700]) .[/COLOR] [COLOR= #DD0000]<br />[/COLOR][COLOR= #007700];[/COLOR]
[COLOR= #FF8000]// Fetchen[/COLOR]
[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]$MySQL->fetch [type = assoc]<br />[/COLOR][COLOR= #007700];[/COLOR]
[URL="http://www.php.net/while"]while[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$fetch[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$MySQL[/COLOR][COLOR= #007700]->[/COLOR]fetch[COLOR= #007700]([/COLOR] [COLOR= #0000BB]$Query[/COLOR] [COLOR= #007700]))
{[/COLOR]
[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #0000BB]$fetch[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]uName[/COLOR][COLOR= #007700]] .[/COLOR] [COLOR= #DD0000]<br />[/COLOR][COLOR= #007700];
}[/COLOR]
[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]Aantal uitgevoerde queries: [/COLOR] [COLOR= #007700].[/COLOR] [COLOR= #0000BB]$MySQL[/COLOR][COLOR= #007700]->[/COLOR]querycount[COLOR= #007700]() .[/COLOR] [COLOR= #DD0000]<br />[/COLOR][COLOR= #007700];[/COLOR]
[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]Tijd benodigd voor uitgevoerde qeuries: [/COLOR] [COLOR= #007700].[/COLOR] [COLOR= #0000BB]$MySQL[/COLOR][COLOR= #007700]->[/COLOR]querytime[COLOR= #007700]() .[/COLOR] [COLOR= #DD0000]<br />[/COLOR][COLOR= #007700];[/COLOR]
[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]Query #1: [/COLOR] [COLOR= #007700].[/COLOR] [COLOR= #0000BB]$MySQL[/COLOR][COLOR= #007700]->[/COLOR]querytime_array[COLOR= #007700]([/COLOR][COLOR= #0000BB]0[/COLOR][COLOR= #007700]) .[/COLOR] [COLOR= #DD0000]<br />[/COLOR][COLOR= #007700];[/COLOR]
?> 



