forked from e-sites/php-mysql-mysqli-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.php
770 lines (659 loc) · 15.5 KB
/
mysql.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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
<?php
/**
* Procedural drop in replacement for legacy projects using the MySQL function
*
* @author Sjoerd Maessen
* @version 0.1+fnx
*/
// Make sure the MySQL extension is not loaded and there is no other drop in replacement active
if(!extension_loaded('mysql') && !function_exists('mysql_connect'))
{
// Validate if the MySQLi extension is present
if(!extension_loaded('mysqli'))
{
trigger_error('The extension "MySQLi" is not available', E_USER_ERROR);
}
// The function name "getLinkIdentifier" will be used to return a valid link_indentifier, make it is available
if(function_exists('getLinkIdentifier'))
{
trigger_error('The function name "getLinkIdentifier" is already defined, please change the function name', E_USER_ERROR);
}
// Define MySQL constants
define('MYSQL_CLIENT_COMPRESS', MYSQLI_CLIENT_COMPRESS);
define('MYSQL_CLIENT_IGNORE_SPACE', MYSQLI_CLIENT_IGNORE_SPACE);
define('MYSQL_CLIENT_INTERACTIVE', MYSQLI_CLIENT_INTERACTIVE);
define('MYSQL_CLIENT_SSL', MYSQLI_CLIENT_SSL);
define('MYSQL_ASSOC', MYSQLI_ASSOC);
define('MYSQL_NUM', MYSQLI_NUM);
define('MYSQL_BOTH', MYSQLI_BOTH);
// Disable exceptions at PHP 8.1
mysqli_report(MYSQLI_REPORT_OFF);
// Will contain the link identifier
$__MYSQLI_WRAPPER_LINK = null;
/**
* Get the link identifier
*
* @param mysqli $mysqli
* @return mysqli|null
*/
function getLinkIdentifier(mysqli $mysqli = null)
{
if(!$mysqli)
{
global $__MYSQLI_WRAPPER_LINK;
$mysqli = $__MYSQLI_WRAPPER_LINK;
}
return $mysqli;
}
/**
* Open a connection to a MySQL Server
*
* @param $server
* @param $username
* @param $password
* @return mysqli|null
*/
function mysql_connect($server, $username, $password, $new_link = false, $client_flags = 0)
{
global $__MYSQLI_WRAPPER_LINK;
$__MYSQLI_WRAPPER_LINK = mysqli_init();
if(($client_flags & MYSQL_CLIENT_SSL) === MYSQL_CLIENT_SSL)
{
mysqli_ssl_set($__MYSQLI_WRAPPER_LINK, $_SERVER['HOME'] . '/.config/mysql/client.key', $_SERVER['HOME'] . '/.config/mysql/client.pem', $_SERVER['HOME'] . '/.config/mysql/ca.pem', null, null);
}
$port = null;
if(($pos = strrpos($server, ':')) !== false)
{
$suffix = substr($server, $pos + 1);
if($suffix === strval(intval($suffix)))
{
$server = substr($server, 0, $pos);
$port = (int) $suffix;
}
}
if(!mysqli_real_connect($__MYSQLI_WRAPPER_LINK, $server, $username, $password, null, $port, null, $client_flags))
{
$__MYSQLI_WRAPPER_LINK = false;
}
return $__MYSQLI_WRAPPER_LINK;
}
/**
* Open a persistent connection to a MySQL server
*
* @param $server
* @param $username
* @param $password
* @return mysqli|null
*/
function mysql_pconnect($server, $username, $password, $client_flags = 0)
{
return mysql_connect('p:' . $server, $username, $password, false, $client_flags);
}
/**
* @param $databaseName
* @return bool
*/
function mysql_select_db($databaseName, mysqli $mysqli = null)
{
return getLinkIdentifier($mysqli)->select_db($databaseName);
}
/**
* @param $query
* @param mysqli $mysqli
* @return bool|mysqli_result
*/
function mysql_query($query, mysqli $mysqli = null)
{
return getLinkIdentifier($mysqli)->query($query);
}
/**
* @param $string
* @param mysqli $mysqli
* @return string
*/
function mysql_real_escape_string($string, mysqli $mysqli = null)
{
return getLinkIdentifier($mysqli)->escape_string($string);
}
/**
* @param $string
* @return string
*/
function mysql_escape_string($string)
{
return mysql_real_escape_string($string);
}
/**
* @param mysqli_result $result
* @return bool|array
*/
function mysql_fetch_assoc(mysqli_result $result)
{
$result = $result->fetch_assoc();
if($result === null)
{
$result = false;
}
return $result;
}
/**
* @param mysqli_result $result
* @return object|stdClass
*/
function mysql_fetch_object(mysqli_result $result)
{
$result = $result->fetch_object();
if($result === null)
{
$result = false;
}
return $result;
}
/**
* @param mysqli_result $result
* @return bool|int
*/
function mysql_num_rows(mysqli_result $result)
{
$result = $result->num_rows;
if($result === null)
{
$result = false;
}
return $result;
}
/**
* @param mysqli_result $result
* @return bool|array
*/
function mysql_fetch_row(mysqli_result $result)
{
$result = $result->fetch_row();
if($result === null)
{
$result = false;
}
return $result;
}
/**
* @param mysqli $mysqli
* @return int
*/
function mysql_affected_rows(mysqli $mysqli = null)
{
return mysqli_affected_rows(getLinkIdentifier($mysqli));
}
/**
* @return void
*/
function mysql_client_encoding(mysqli $mysqli = null)
{
return mysqli_character_set_name(getLinkIdentifier($mysqli));
}
/**
* @param mysqli $mysqli
* @return bool
*/
function mysql_close(mysqli $mysqli = null)
{
return mysqli_close(getLinkIdentifier($mysqli));
}
/**
* @return bool
*/
function mysql_create_db($database_name, mysqli $mysqli = null)
{
trigger_error('This function was deprecated in PHP 4.3.0 and is therefor not supported', E_USER_DEPRECATED);
return false;
}
/**
* @param mysqli $mysqli
* @return int
*/
function mysql_errno(mysqli $mysqli = null)
{
return mysqli_errno(getLinkIdentifier($mysqli));
}
/**
* Adjusts the result pointer to an arbitrary row in the result
*
* @param $result
* @param $row
* @param int $field
* @return bool
*/
function mysql_db_name(mysqli_result $result, $row, $field = null)
{
mysqli_data_seek($result,$row);
$f = mysqli_fetch_row($result);
return $f[0];
}
/**
* @param mysqli $mysqli
* @return string
*/
function mysql_error(mysqli $mysqli = null)
{
return mysqli_error(getLinkIdentifier($mysqli));
}
/**
* @param mysqli_result $result
* @param $result_type
* @return void
*/
function mysql_fetch_array(mysqli_result $result, $result_type = MYSQL_BOTH)
{
return mysqli_fetch_array($result, $result_type);
}
/**
* @param mysqli $mysqli
* @return bool
*/
function mysql_ping(mysqli $mysqli = null)
{
return mysqli_ping(getLinkIdentifier($mysqli));
}
/**
* @param $query
* @param mysqli $mysqli
*/
function mysql_unbuffered_query($query, mysqli $mysqli = null)
{
return mysqli_query(getLinkIdentifier($mysqli), $query, MYSQLI_USE_RESULT);
}
/**
* @return string
*/
function mysql_get_client_info()
{
global $__MYSQLI_WRAPPER_LINK;
return mysqli_get_client_info($__MYSQLI_WRAPPER_LINK);
}
/**
* @param mysqli_result $result
* @return void
*/
function mysql_free_result(mysqli_result $result)
{
mysqli_free_result($result);
return true;
}
/**
* @param mysqli $mysqli
* @return bool|mysqli_result
*/
function mysql_list_dbs(mysqli $mysqli = null)
{
trigger_error('This function is deprecated. It is preferable to use mysql_query() to issue an SQL Query: SHOW DATABASES statement instead.', E_USER_DEPRECATED);
return mysqli_query(getLinkIdentifier($mysqli), 'SHOW DATABASES');
}
/**
* @param $database_name
* @param $table_name
* @param null $mysqli
* @return bool|mysqli_result
*/
function mysql_list_fields($database_name, $table_name, mysqli $mysqli = null)
{
trigger_error('This function is deprecated. It is preferable to use mysql_query() to issue an SQL SHOW COLUMNS FROM table [LIKE \'name\'] statement instead.', E_USER_DEPRECATED);
$mysqli = getLinkIdentifier($mysqli);
$db = mysqli_escape_string($mysqli, $database_name);
$table = mysqli_escape_string($mysqli, $table_name);
return mysqli_query($mysqli, sprintf('SHOW COLUMNS FROM %s.%s', $db, $table));
}
/**
* @param mysqli $mysqli
* @return bool|mysqli_result
*/
function mysql_list_processes(mysqli $mysqli = null)
{
return mysqli_query(getLinkIdentifier($mysqli), 'SHOW PROCESSLIST');
}
/**
* @param $charset
* @param null $mysqli
* @return bool
*/
function mysql_set_charset($charset, mysqli $mysqli = null)
{
return mysqli_set_charset(getLinkIdentifier($mysqli), $charset);
}
/**
* @param null $mysqli
* @return bool|string
*/
function mysql_info(mysqli $mysqli = null)
{
$result = mysqli_info(getLinkIdentifier($mysqli));
if($result === null)
{
$result = false;
}
return $result;
}
/**
* Get current system status
*
* @param null $mysqli
* @return bool|string
*/
function mysql_stat(mysqli $mysqli = null)
{
return mysqli_stat(getLinkIdentifier($mysqli));
}
/**
* Return the current thread ID
*
* @param null $mysqli
* @return bool|string
*/
function mysql_thread_id(mysqli $mysqli = null)
{
return mysqli_thread_id(getLinkIdentifier($mysqli));
}
/**
* Get MySQL host info
*
* @param null $mysqli
* @return bool|string
*/
function mysql_get_host_info(mysqli $mysqli = null)
{
return mysqli_get_host_info(getLinkIdentifier($mysqli));
}
/**
* Get MySQL protocol info
*
* @param null $mysqli
* @return bool|string
*/
function mysql_get_proto_info(mysqli $mysqli = null)
{
return mysqli_get_proto_info(getLinkIdentifier($mysqli));
}
/**
* Get MySQL server info
*
* @param null $mysqli
* @return bool|string
*/
function mysql_get_server_info(mysqli $mysqli = null)
{
return mysqli_get_server_info(getLinkIdentifier($mysqli));
}
/**
* Get table name of field
*
* @param $result
* @param $row
* @return bool
*/
function mysql_tablename(mysqli_result $result, $row)
{
mysqli_data_seek($result, $row);
$f = mysqli_fetch_array($result);
return $f[0];
}
/**
* Get the ID generated in the last query
*
* @param null $mysqli
* @return int|string
*/
function mysql_insert_id(mysqli $mysqli = null)
{
return mysqli_insert_id(getLinkIdentifier($mysqli));
}
/**
* Get result data
*
* @param $result
* @param $row
* @param int $field
* @return mixed
*/
function mysql_result($result, $row, $field = 0)
{
$result->data_seek($row);
$row = $result->fetch_array();
if(!isset($row[$field]))
{
return false;
}
return $row[$field];
}
/**
* Get number of fields in result
*
* @param mysqli_result $result
* @return int
*/
function mysql_num_fields(mysqli_result $result)
{
return mysqli_num_fields($result);
}
/**
* List tables in a MySQL database
*
* @param null $mysqli
* @return bool|string
*/
function mysql_list_tables($database_name, mysqli $mysqli = null)
{
trigger_error('This function is deprecated. It is preferable to use mysql_query() to issue an SQL SHOW TABLES [FROM db_name] [LIKE \'pattern\'] statement instead.', E_USER_DEPRECATED);
$mysqli = getLinkIdentifier($mysqli);
$db = mysqli_escape_string($mysqli, $database_name);
return mysqli_query($mysqli, sprintf('SHOW TABLES FROM %s', $db));
}
/**
* Get column information from a result and return as an object
*
* @param mysqli_result $result
* @param int $field_offset
* @return bool|object
*/
function mysql_fetch_field(mysqli_result $result, $field_offset = 0)
{
if($field_offset)
{
mysqli_field_seek($result, $field_offset);
}
return mysqli_fetch_field($result);
}
/**
* Returns the length of the specified field
*
* @param mysqli_result $result
* @param int $field_offset
* @return bool
*/
function mysql_field_len(mysqli_result $result, $field_offset = 0)
{
$fieldInfo = mysqli_fetch_field_direct($result, $field_offset);
return $fieldInfo->length;
}
/**
* @return bool
*/
function mysql_drop_db()
{
trigger_error('This function is deprecated since PHP 4.3.0 and therefore not implemented', E_USER_DEPRECATED);
return false;
}
/**
* Move internal result pointer
*
* @param mysqli_result $result
* @param int $row_number
* @return void
*/
function mysql_data_seek(mysqli_result $result, $row_number = 0)
{
return mysqli_data_seek($result, $row_number);
}
/**
* Get the name of the specified field in a result
*
* @param $result
* @param $field_offset
* @return bool
*/
function mysql_field_name($result, $field_offset = 0)
{
$props = mysqli_fetch_field_direct($result, $field_offset);
return (is_object($props) ? $props->name : false);
}
/**
* Get the length of each output in a result
*
* @param mysqli_result $result
* @return array|bool
*/
function mysql_fetch_lengths(mysqli_result $result)
{
return mysqli_fetch_lengths($result);
}
/**
* Get the type of the specified field in a result
* @param mysqli_result $result
* @param $field_offset
* @return string
*/
function mysql_field_type(mysqli_result $result, $field_offset = 0)
{
$unknown = 'unknown';
$info = mysqli_fetch_field_direct($result, $field_offset);
if(empty($info->type))
{
return $unknown;
}
switch ($info->type)
{
case MYSQLI_TYPE_FLOAT:
case MYSQLI_TYPE_DOUBLE:
case MYSQLI_TYPE_DECIMAL:
case MYSQLI_TYPE_NEWDECIMAL:
return 'real';
case MYSQLI_TYPE_BIT:
return 'bit';
case MYSQLI_TYPE_TINY:
return 'tinyint';
case MYSQLI_TYPE_TIME:
return 'time';
case MYSQLI_TYPE_DATE:
return 'date';
case MYSQLI_TYPE_DATETIME:
return 'datetime';
case MYSQLI_TYPE_TIMESTAMP:
return 'timestamp';
case MYSQLI_TYPE_YEAR:
return 'year';
case MYSQLI_TYPE_STRING:
case MYSQLI_TYPE_VAR_STRING:
return 'string';
case MYSQLI_TYPE_SHORT:
case MYSQLI_TYPE_LONG:
case MYSQLI_TYPE_LONGLONG:
case MYSQLI_TYPE_INT24:
return 'int';
case MYSQLI_TYPE_CHAR:
return 'char';
case MYSQLI_TYPE_ENUM:
return 'enum';
case MYSQLI_TYPE_TINY_BLOB:
case MYSQLI_TYPE_MEDIUM_BLOB:
case MYSQLI_TYPE_LONG_BLOB:
case MYSQLI_TYPE_BLOB:
return 'blob';
case MYSQLI_TYPE_NULL:
return 'null';
#case MYSQLI_TYPE_NEWDATE:
#case MYSQLI_TYPE_INTERVAL:
#case MYSQLI_TYPE_SET:
#case MYSQLI_TYPE_GEOMETRY:
default:
return $unknown;
}
}
/**
* Get name of the table the specified field is in
*
* @param mysqli_result $result
* @param $field_offset
* @return bool
*/
function mysql_field_table(mysqli_result $result, $field_offset = 0)
{
$info = mysqli_fetch_field_direct($result, $field_offset);
if(empty($info->table))
{
return false;
}
return $info->table;
}
/**
* Get the flags associated with the specified field in a result
*
* credit to Dave Smith from phpclasses.org, andre at koethur dot de from php.net and NinjaKC from stackoverflow.com
*
* @param mysqli_result $result
* @param int $field_offset
* @return bool
*/
function mysql_field_flags(mysqli_result $result , $field_offset = 0)
{
$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) && !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;
}
/**
* Set result pointer to a specified field offset
*
* @param mysqli_result $result
* @param int $field_offset
* @return bool
*/
function mysql_field_seek(mysqli_result $result, $field_offset = 0)
{
return mysqli_field_seek($result, $field_offset);
}
/**
* Selects a database and executes a query on it
*
* @param $database
* @param $query
* @param mysqli $mysqli
* @return bool
*/
function mysql_db_query($database, $query, mysqli $mysqli = null)
{
if(mysql_select_db($database, $mysqli))
{
return mysql_query($query, $mysqli);
}
return false;
}
}
?>