-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql2i.class.php
544 lines (357 loc) · 14.5 KB
/
mysql2i.class.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
<?php
/*
mysql2i.class.php ver 1.5
#fixed statement structure to support legacy versions of PHP older than 5.5
#updated mysql_result
#fixed mysql_fetch_object so third param defaults to an array
ver 1.0
Initial release
This class is released into the public domain without copyright
*/
class mysql2i{
public static $currObj;
public static function mysql_affected_rows($link=null){
if( empty($link) ){
$link = self::$currObj;
}
return mysqli_affected_rows($link);
}
public static function mysql_client_encoding($link=null){
if( empty($link) ){
$link = self::$currObj;
}
return mysqli_character_set_name($link);
}
public static function mysql_close($link=null){
if( empty($link) ){
$link = self::$currObj;
}
return mysqli_close($link);
}
public static function mysql_connect($host = '',$username = '',$passwd = '',$new_link = false,$client_flags = 0){
$link = mysqli_connect($host,$username,$passwd);
self::$currObj = $link;
return $link;
}
public static function mysql_create_db($database_name,$link=null){
if( empty($link) ){
$link = self::$currObj;
}
$query = "CREATE DATABASE `".$database_name."`";
mysqli_query($link,$query);
$e = mysqli_errno($link);
if( empty($e) ){
return true;
}else{
return false;
}
}
public static function mysql_data_seek($result,$offset){
return mysqli_data_seek($result,$offset);
}
public static function mysql_db_name($result,$row,$field=null){
mysqli_data_seek($result,$row);
$f = mysqli_fetch_row($result);
return $f[0];
}
public static function mysql_db_query($database,$query,$link=null){
if( empty($link) ){
$link = self::$currObj;
}
mysqli_select_db($link,$database);
$r = mysqli_query($link,$query);
return $r;
}
public static function mysql_drop_db($database,$link=null){
if( empty($link) ){
$link = self::$currObj;
}
$query = "DROP DATABASE `".$database."`";
mysqli_query($link,$query);
$e = mysqli_errno($link);
if( empty($e) ){
return true;
}else{
return false;
}
}
public static function mysql_errno($link=null){
if( empty($link) ){
$link = self::$currObj;
}
return mysqli_errno($link);
}
public static function mysql_error($link=null){
if( empty($link) ){
$link = self::$currObj;
}
return mysqli_error($link);
}
public static function mysql_escape_string($escapestr){
if( empty($link) ){
$link = self::$currObj;
}
return mysqli_real_escape_string($link,$escapestr);
}
public static function mysql_fetch_array($result,$resulttype=MYSQLI_BOTH){
return mysqli_fetch_array($result,$resulttype);
}
public static function mysql_fetch_assoc($result){
return mysqli_fetch_assoc($result);
}
public static function mysql_fetch_field($result,$field_offset=0){
if( !empty($field_offset) ){
for($x=0;$x<$field_offset;$x++){
mysqli_fetch_field($result);
}
}
return mysqli_fetch_field($result);
}
public static function mysql_fetch_lengths($result){
return mysqli_fetch_lengths($result);
}
public static function mysql_fetch_object($result,$class_name=null,$params=array()){
return mysqli_fetch_object($result,$class_name,$params);
}
public static function mysql_fetch_row($result){
return mysqli_fetch_row($result);
}
/*
credit to andre at koethur dot de from php.net and NinjaKC from stackoverflow.com
*/
public static function mysql_field_flags( $result , $field_offset ) {
static $flags;
$flags_num = mysqli_fetch_field_direct($result,$field_offset)->flags;
if (!isset($flags))
{
$flags = array();
$constants = get_defined_constants(true);
foreach ($constants['mysqli'] as $c => $n) if (preg_match('/MYSQLI_(.*)_FLAG$/', $c, $m)) if (!array_key_exists($n, $flags)) $flags[$n] = $m[1];
}
$result = array();
foreach ($flags as $n => $t) if ($flags_num & $n) $result[] = $t;
$return = implode(' ', $result);
$return = str_replace('PRI_KEY','PRIMARY_KEY',$return);
$return = strtolower($return);
return $return;
}
public static function mysql_field_len($result,$field_offset){
$fieldInfo = mysqli_fetch_field_direct($result,$field_offset);
return $fieldInfo->length;
}
public static function mysql_field_name($result,$field_offset){
$fieldInfo = mysqli_fetch_field_direct($result,$field_offset);
return $fieldInfo->name;
}
public static function mysql_field_seek($result,$fieldnr){
return mysqli_field_seek($result,$fieldnr);
}
public static function mysql_field_table($result,$field_offset){
$fieldInfo = mysqli_fetch_field_direct($result,$field_offset);
return $fieldInfo->table;
}
/*
credit to andre at koethur dot de from php.net and NinjaKC from stackoverflow.com
*/
public static function mysql_field_type( $result , $field_offset ) {
static $types;
$type_id = mysqli_fetch_field_direct($result,$field_offset)->type;
if (!isset($types))
{
$types = array();
$constants = get_defined_constants(true);
foreach ($constants['mysqli'] as $c => $n) if (preg_match('/^MYSQLI_TYPE_(.*)/', $c, $m)) $types[$n] = $m[1];
}
return array_key_exists($type_id, $types)? $types[$type_id] : NULL;
}
public static function mysql_free_result($result){
return mysqli_free_result($result);
}
public static function mysql_get_client_info(){
$link = self::$currObj;
return mysqli_get_client_info($link);
}
public static function mysql_get_host_info($link=null){
if( empty($link) ){
$link = self::$currObj;
}
return mysqli_get_host_info($link);
}
public static function mysql_get_proto_info($link=null){
if( empty($link) ){
$link = self::$currObj;
}
return mysqli_get_proto_info($link);
}
public static function mysql_get_server_info($link=null){
if( empty($link) ){
$link = self::$currObj;
}
return mysqli_get_server_info($link);
}
public static function mysql_info($link=null){
if( empty($link) ){
$link = self::$currObj;
}
return mysqli_info($link);
}
public static function mysql_insert_id($link=null){
if( empty($link) ){
$link = self::$currObj;
}
return mysqli_insert_id($link);
}
public static function mysql_list_dbs($link=null){
if( empty($link) ){
$link = self::$currObj;
}
$query = "SHOW DATABASES";
$r = mysqli_query($link,$query);
$e = mysqli_errno($link);
if( empty($e) ){
return $r;
}else{
return false;
}
}
public static function mysql_list_fields($database_name,$table_name,$link=null){
if( empty($link) ){
$link = self::$currObj;
}
$query = "SHOW COLUMNS FROM `".$table_name."`";
$r = mysqli_query($link,$query);
$e = mysqli_errno($link);
if( empty($e) ){
return $r;
}else{
return false;
}
}
public static function mysql_list_processes($link=null){
if( empty($link) ){
$link = self::$currObj;
}
return mysqli_thread_id($link);
}
public static function mysql_list_tables($database,$link=null){
if( empty($link) ){
$link = self::$currObj;
}
$query = "SHOW TABLES FROM `".$database."`";
$r = mysqli_query($link,$query);
$e = mysqli_errno($link);
if( empty($e) ){
return $r;
}else{
return false;
}
}
public static function mysql_num_fields($result){
$link = self::$currObj;
return mysqli_field_count($link);
}
public static function mysql_num_rows($result){
return mysqli_num_rows($result);
}
public static function mysql_pconnect($host = '',$username = '',$passwd = '',$new_link = false,$client_flags = 0){
$link = mysqli_connect('p:'.$host,$username,$passwd);
if( !$link ){
echo mysqli_error($link).'<br>';
}
self::$currObj = $link;
return $link;
}
public static function mysql_ping($link=null){
if( empty($link) ){
$link = self::$currObj;
}
return mysqli_ping($link);
}
public static function mysql_query($query,$link=null){
if( empty($link) ){
$link = self::$currObj;
}
$r = mysqli_query($link,$query);
return $r;
}
public static function mysql_real_escape_string($escapestr,$link=null){
if( empty($link) ){
$link = self::$currObj;
}
return mysqli_real_escape_string($link,$escapestr);
}
public static function mysql_result($result,$row,$field=0){
//ver 1.5 code credited to Mario Lurig at https://mariolurig.com/coding/mysqli_result-function-to-match-mysql_result/
$numrows = mysqli_num_rows($result);
if( $numrows && $row <= ($numrows-1) && $row >=0 ){
mysqli_data_seek($result,$row);
$resrow = ( is_numeric($field) ) ? mysqli_fetch_row($result) : mysqli_fetch_assoc($result);
if ( isset($resrow[$field]) ){
return $resrow[$field];
}
}
return false;
/*
//ver 1.0 code has problems with pointer and does not support numeric offset
mysqli_data_seek($result,$row);
if( !empty($field) ){
while($finfo = mysqli_fetch_field($result)) {
if( $field == $finfo->name ){
$f = mysqli_fetch_assoc($result);
return $f[$field];
}
}
}
$f = mysqli_fetch_array($result);
return $f[0];
*/
}
public static function mysql_select_db($dbname,$link=null){
if( empty($link) ){
$link = self::$currObj;
}elseif( is_object($link) ){
self::$currObj = $link;
}
mysqli_select_db($link, $dbname);
$e = mysqli_errno($link);
if( empty($e) ){
return true;
}else{
return false;
}
}
public static function mysql_set_charset($charset,$link=null){
if( empty($link) ){
$link = self::$currObj;
}
return mysqli_set_charset($link,$charset);
}
public static function mysql_stat($link=null){
if( empty($link) ){
$link = self::$currObj;
}
return mysqli_stat($link);
}
public static function mysql_tablename($result,$row,$field=null){
mysqli_data_seek($result,$row);
$f = mysqli_fetch_array($result);
return $f[0];
}
public static function mysql_thread_id($link=null){
if( empty($link) ){
$link = self::$currObj;
}
return mysqli_thread_id($link);
}
public static function mysql_unbuffered_query($query,$link=null){
if( empty($link) ){
$link = self::$currObj;
}
$r = mysqli_query($link,$query,MYSQLI_USE_RESULT);
return $r;
}
}
if( !extension_loaded('mysql') ){
include('mysql2i.func.php');
}
?>