-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwdgwv_sql.php
executable file
·499 lines (432 loc) · 13.2 KB
/
wdgwv_sql.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
<?php
/*
:....................,:,
,.`,,,::;;;;;;;;;;;;;;;;:;`
`...`,::;:::::;;;;;;;;;;;;;::'
,..``,,,::::::::::::::::;:;;:::;
:.,,``..::;;,,,,,,,,,,,,,:;;;;;::;`
,.,,,`...,:.:,,,,,,,,,,,,,:;:;;;;:;;
`..,,``...;;,;::::::::::::::'';';';:''
,,,,,``..:;,;;:::::::::::::;';;';';;'';
,,,,,``....;,,:::::::;;;;;;;;':'''';''+;
:,::```....,,,:;;;;;;;;;;;;;;;''''';';';;
`,,::``.....,,,;;;;;;;;;;;;;;;;'''''';';;;'
:;:::``......,;;;;;;;;:::::;;;;'''''';;;;:
;;;::,`.....,::;;::::::;;;;;;;;'''''';;,;;,
;:;;:;`....,:::::::::::::::::;;;;'''':;,;;;
';;;;;.,,,,::::::::::::::::::;;;;;''':::;;'
;';;;;.;,,,,::::::::::::::::;;;;;;;''::;;;'
;'';;:;..,,,;;;:;;:::;;;;;;;;;;;;;;;':::;;'
;'';;;;;.,,;:;;;;;;;;;;;;;;;;;;;;;;;;;:;':;
;''';;:;;.;;;;;;;;;;;;;;;;;;;;;;;;;;;''';:.
:';';;;;;;::,,,,,,,,,,,,,,:;;;;;;;;;;'''';
'';;;;:;;;.,,,,,,,,,,,,,,,,:;;;;;;;;'''''
'''';;;;;:..,,,,,,,,,,,,,,,,,;;;;;;;''':,
.'''';;;;....,,,,,,,,,,,,,,,,,,,:;;;''''
''''';;;;....,,,,,,,,,,,,,,,,,,;;;''';.
'''';;;::.......,,,,,,,,,,,,,:;;;''''
`''';;;;:,......,,,,,,,,,,,,,;;;;;''
.'';;;;;:.....,,,,,,,,,,,,,,:;;;;'
`;;;;;:,....,,,,,,,,,,,,,,,:;;''
;';;,,..,.,,,,,,,,,,,,,,,;;',
'';:,,,,,,,,,,,,,,,::;;;:
`:;'''''''''''''''';:.
,,,::::::::::::::::::::::::;;;;,::::::::::::::::::::::::
,::::::::::::::::::::::::::;;;;,::::::::::::::::::::::::
,:; ## ## ## ##### #### ## ## ## ## ## ;::
,,; ## ## ## ## ## ## ## ## ## ## ## ;::
,,; ## ## ## ## ## ## #### ## ## ## ## ## ;::
,,' ## ## ## ## ## ## ## ## ## ## ## ## :::
,:: ######## #### ###### ######## ### :::
,,,:,,:,,:::,,,:;:::::::::::::::;;;:::;:;:::::::::::::::
,,,,,,,,,,,,,,,,,,,,,,,,:,::::::;;;;:::::;;;;::::;;;;:::
(c) WDGWV. 2013, http://www.wdgwv.com
websites, Apps, Hosting, Services, Development.
File Checked.
Checked by: WdG.
File created: WdG.
date: 27-JAN-2014
Last update: 24-MAR-2014
© WDGWV, www.wdgwv.com
All Rights Reserved.
*/
/*
## DISCLAIMER // INFO ##
This set of functions is basicly to support the deprecated mysql_* functions
So if you replace mysql_* with wdgwv_sql_* it will work again.
The wdgwv_sql_* is written in pdo, and is only meaned for temporary!
This code is written by WdG (Wesley de Groot)
This code is tested by WdG and EH (Edwin Huijboom)
This code is provided by WDGWV
more info? http://wdgwv.github.io/MySQL_Support.html
## END OF DISCLAIMER // INFO ##
*/
// Config & Objects
$wdgwv_sql = array();
$wdgwv_sql['config']['trigger_error'] = false;
$wdgwv_sql['config']['fetch_type'] = null; //PDO::FETCH_ASSOC (names)
//PDO::FETCH_NUM (array 0,1,2,3,etc)
$wdgwv_sql['error'] = null; //WGT: FIX: 29-JAN-2014
#function wdgwv_sql_close ( res )
# Replaces mysql_close( res ).
## WdG: 29 JAN 2014
function wdgwv_sql_close ( $res )
{
global $wdgwv_sql;
$wdgwv_sql['connection'] = null;
$wdgwv_sql['connect'] = null;
$wdgwv_sql['error'] = null;
}
#function wdgwv_sql_real_escape_string ( string )
# Replaces mysql_real_escape_string.
## WdG: 29 JAN 2014
function wdgwv_sql_real_escape_string ( $string )
{
$return = '';
for($i = 0; $i < strlen($string); ++$i) {
$char = $string[$i];
$ord = ord($char);
if($char !== "'" && $char !== "\"" && $char !== '\\' && $ord >= 32 && $ord <= 126)
$return .= $char;
else
$return .= '\\x' . dechex($ord);
}
return $return;
}
#function wdgwv_sql_escape_string ( string )
# Replaces mysql_escape_string.
## WdG: 29 JAN 2014
function wdgwv_sql_escape_string ( $string )
{
$return = '';
for($i = 0; $i < strlen($string); ++$i) {
$char = $string[$i];
$ord = ord($char);
if($char !== "'" && $char !== "\"" && $char !== '\\' && $ord >= 32 && $ord <= 126)
$return .= $char;
else
$return .= '\\x' . dechex($ord);
}
return $return;
}
#function wdgwv_sql_trigger_error ( error )
# Replaces none, checks if needed to trigger a error otherwise save it in $wdgwv_sql.
## WdG: 27 JAN 2014
function wdgwv_sql_trigger_error ($error)
{
//Load global config
global $wdgwv_sql;
//what to do? reset or make a error?
if ( $error == false)
{
//reset the error.
$wdgwv_sql['old_error'] = $wdgwv_sql['error'];
$wdgwv_sql['error'] = null;
}
else
{
// need to trigger the error?
if ( $wdgwv_sql['config']['trigger_error'] )
{
//yes, trigger the error.
trigger_error($error);
}
else
{
//nope, save the error only...
$wdgwv_sql['old_error'] = $wdgwv_sql['error'];
$wdgwv_sql['error'] = $error;
}
}
}
#function wdgwv_sql_sql_connect(hostname, username, password)
# Replaces mysql_connect(hostname, username, password)
## WdG: 27 JAN 2014
function wdgwv_sql_connect($hostname, $username, $password)
{
//Load global config
global $wdgwv_sql;
wdgwv_sql_trigger_error(false);
//Set some parameters
$wdgwv_sql['connect']['hostname'] = $hostname;
$wdgwv_sql['connect']['username'] = $username;
$wdgwv_sql['connect']['password'] = $password;
//return: true (connection)
// couse pdo need database for connect.
return true;
}
#function wdgwv_sql_select_db(database, link = null )
# Replaces mysql_select_db(database, link)
## WdG: 27 JAN 2014
function wdgwv_sql_select_db($database, $link = null)
{
//Load global config
global $wdgwv_sql;
wdgwv_sql_trigger_error(false);
//Set database name
$wdgwv_sql['connect']['database'] = $database;
//we do nothing with $link.
try
{
//Let's connect...
$wdgwv_sql['connection'] = new PDO(
'mysql:host=' . //Say use mysql with host..
$wdgwv_sql['connect']['hostname'] . // <-- HOST
';dbname=' . //And database...
$wdgwv_sql['connect']['database'] . // <-- DATABASE
';charset:utf8', //With Charset utf8.
$wdgwv_sql['connect']['username'], // <-- USERNAME.
$wdgwv_sql['connect']['password'] // <-- PASSWORD.
);
}
catch(PDOException $e)
{
return false;
}
if ( @$wdgwv_sql['connection'] ) //if connected then..
{
// set the PDO modes...
$wdgwv_sql['connection'] -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$wdgwv_sql['connection'] -> setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
//and return i'm connected
return true;
}
else
{
// Failed... trigger a error
wdgwv_sql_trigger_error('Can\'t Connect or Select database');
// And sent i did'nt connect...
return false;
}
}
#function wdgwv_sql_error( )
# Replaces mysql_error( )
## WdG: 27 JAN 2014
function wdgwv_sql_error ( )
{
//Load global config
global $wdgwv_sql;
// Is there a error?
if ( isset ( $wdgwv_sql['error'] ) )
return $wdgwv_sql['error']; //Yes, so return it back
else
return null; //Nope error is null.
}
#function wdgwv_sql_query( query, link = null )
# Replaces mysql_query( query, link )
## WdG: 27 JAN 2014
## WdG: 24 MAR 2014: Fix: Now catch and triggers error!
function wdgwv_sql_query ( $query, $link = null )
{
//Load global config
global $wdgwv_sql;
wdgwv_sql_trigger_error(false); //Reset errors
$wdgwv_sql['last']['query'] = $query; //Saves last query...
try {
// Execute and return command...
$cmd = (
@$wdgwv_sql['connection'] -> query ( $query )
// ^ Query...
);
} catch (PDOException $ex) {
wdgwv_sql_trigger_error('Query: ' . $query . ' Was not executed, ' . $ex);
}
// Is the command executed good?
if ( $cmd )
{
return $cmd; //Yes, so return the Query data!
}
else
{
// No trigger a error...
wdgwv_sql_trigger_error('Query: ' . $query . ' Was not executed, please check your query');
// and return i've a error.
return false;
}
}
#function wdgwv_sql_fetch_array ( query, foreach, link = null )
# Replaces mysql_fetch_array ( query, link )
## WdG: 27 JAN 2014
function wdgwv_sql_fetch_array( $command, $foreach = false, $type = null )
{
//Load global config
global $wdgwv_sql;
wdgwv_sql_trigger_error(false);
//Create a temporary array.
$tempArray = array();
while ( $row = ( @$command->fetch ( $wdgwv_sql['config']['fetch_type'] ) ) )
// Load the data, but please ignore errors!
{
// Put the data to the temporary array.
$tempArray[] = $row;
}
// if the size is zero, i think it's a error, so i trigger a error!
if ( sizeof($tempArray) == 0 )
{
//error so trigger a error.
wdgwv_sql_trigger_error('Error can\'t fetch data.');
// return false we got nothing?!
return false;
}
else
{
if ( sizeof($tempArray) == 1) //WGT: Fix 29-JAN-2014 one item then not needed to
{
if (!$foreach)
$ret = $tempArray[0]; //!in a foreach.... ;)
else
$ret = $tempArray; //in a foreach!!!!
return $ret; // have $arr[0][SELECTED ITEMS]
}
else
{
return $tempArray; //no error, so just return the data.
}
}
unset($tempArray); // unset the temporary array to clean up the mess.
}
#function wdgwv_sql_fetch_assoc ( query, foreach )
# Replaces mysql_fetch_assoc ( query )
## WdG: 27 JAN 2014
function wdgwv_sql_fetch_assoc ( $command, $foreach = false, $type = null )
{
//Load global config
global $wdgwv_sql;
wdgwv_sql_trigger_error(false);
//Create a temporary array.
$tempArray = array();
while ( $row = ( @$command->fetch ( PDO::FETCH_ASSOC ) ) )
// Load the data, but please ignore errors!
{
// Put the data to the temporary array.
$tempArray[] = $row;
}
// if the size is zero, i think it's a error, so i trigger a error!
if ( sizeof($tempArray) == 0 )
{
//error so trigger a error.
wdgwv_sql_trigger_error('Error can\'t fetch data.');
// return false we got nothing?!
return false;
}
else
{
if ( sizeof($tempArray) == 1) //WGT: Fix 29-JAN-2014 one item then not needed to
{
if (!$foreach)
$ret = $tempArray[0]; //!in a foreach.... ;)
else
$ret = $tempArray; //in a foreach!!!!
return $ret; // have $arr[0][SELECTED ITEMS]
}
else
return $tempArray; //no error, so just return the data.
}
unset($tempArray); // unset the temporary array to clean up the mess.
}
#function wdgwv_sql_fetch_row ( query, type = null )
# Replaces mysql_fetch_row ( query, type )
## WdG: 27 JAN 2014
function wdgwv_sql_fetch_row( $command, $type = null )
{
//Load global config
global $wdgwv_sql;
wdgwv_sql_trigger_error(false);
//Create a temporary array.
$tempArray = array();
while ( $row = ( @$command->fetch ( $wdgwv_sql['config']['fetch_type'] ) ) )
// Load the data, but please ignore errors!
{
// Put the data to the temporary array.
$tempArray[] = $row;
}
// if the size is zero, i think it's a error, so i trigger a error!
if ( sizeof($tempArray) == 0 )
{
//error so trigger a error.
wdgwv_sql_trigger_error('Error can\'t fetch data.');
// return false we got nothing?!
return false;
}
else
{
return $tempArray[0]; //no error, so just return the data.
}
unset($tempArray); // unset the temporary array to clean up the mess.
}
#function wdgwv_sql_num_rows ( query, link = null )
# Replaces mysql_num_rows ( query, link )
## WdG: 27 JAN 2014
function wdgwv_sql_num_rows ( $query, $link = null )
{
//Load global config
global $wdgwv_sql;
//Don't know if error reset is neccecary.
wdgwv_sql_trigger_error(false);
//num the rows
$num_rows = ( $query -> rowCount ( ) );
//is there a error?!
if ($num_rows)
{
//no error, return the count...
return $num_rows;
}
else
{
//ERROR, trigger a error
wdgwv_sql_error('Can\'t count rows');
//error so return false.
return false;
}
}
#function wdgwv_sql_insert_id ( query = null )
# Replaces mysql_insert_id ( query )
## WdG: 27 JAN 2014
function wdgwv_sql_insert_id ( $query = null )
{
//Load global config
global $wdgwv_sql;
//Don't know if error reset is neccecary.
wdgwv_sql_trigger_error(false);
//num the rows
$lastInsertedId = ( @$wdgwv_sql['connection'] -> lastInsertId ( ) );
//is there a error?!
if ($lastInsertedId)
{
//no error, return the count...
return $lastInsertedId;
}
else
{
//ERROR, trigger a error
wdgwv_sql_error('Can\'t fetch id of last row');
//error so return false.
return false;
}
}
#function wdgwv_sql_affected_rows ( query = null )
# Replaces mysql_affected_rows ( query )
## WdG: 27 JAN 2014
function wdgwv_sql_affected_rows ( $query = null )
{
//Load global config
global $wdgwv_sql;
//Don't know if error reset is neccecary.
wdgwv_sql_trigger_error(false);
//num the rows
$affected_rows = ( @$wdgwv_sql['connection'] -> lastInsertId ( ) );
//is there a error?!
if ($affected_rows)
{
//no error, return the count...
return $affected_rows;
}
else
{
//ERROR, trigger a error
wdgwv_sql_error('Can\'t get affected rows');
//error so return false.
return false;
}
}
?>