-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmysql-wrapper.php
1796 lines (1632 loc) · 51.9 KB
/
mysql-wrapper.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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* This is deprecated MySQL API wrapper
*
* Author: JoungKyun.Kim <http://oops.org>
* Copyright (c) 2016 JoungKyun.Kim
* License: BSD 2-Clause
*
* $Id: $
*
* Warning!
*
* 1. You must enable mysqli extension.
* 2. PHP 4.1 <= PHP VERSION <= PHP7
* 2. If you check return value by mysql_connect and mysql_result with is_resource(),
* you must change to is_myresource() from is_resource().
*
*/
// {{{ +-- public (bool) is_myresource ($o, $onlyres = false)
/**
* @param $o
* @param bool $onlyres
* @return bool
*/
function is_myresource ($o, $onlyres = false) {
if ( extension_loaded ('mysql') ) {
if ( ! is_resource ($o) )
return false;
$cname = get_resource_type ($o);
if ( $cname != 'mysql link' && $cname != 'mysql result' )
return false;
if ( $onlyres && $cname != 'mysql result' )
return false;
unset ($cname);
return true;
}
if ( ! is_object ($o) )
return false;
$cname = get_class ($o);
if ( $cname != 'mysqli' && $cname != 'mysqli_result' )
return false;
if ( $onlyres && $cname != 'mysqli_result' )
return false;
unset ($cname);
return true;
}
// }}}
if ( ! function_exists ('mysql_connect') ) {
if ( ! extension_loaded ('mysqli') ) {
throw new Exception (E_ERROR, 'MySQL wrapper must need mysqli extension');
}
if ( ! function_exists ('___ini_get') ) {
function ___ini_get ($v) { return ini_get ($v); }
}
$_MySQLCON_ = null;
$_MyConnErr_ = null;
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_BOTH', MYSQLI_BOTH);
define ('MYSQL_NUM', MYSQLI_NUM);
if ( ! defined ('MYSQLI_NO_DEFAULT_VALUE_FLAG') )
define ('MYSQLI_NO_DEFAULT_VALUE_FLAG', -1);
if ( ! defined ('MYSQLI_ON_UPDATE_NOW_FLAG') )
define ('MYSQLI_ON_UPDATE_NOW_FLAG', -1);
$msyql_filed_flags = array (
MYSQLI_NOT_NULL_FLAG,
MYSQLI_PRI_KEY_FLAG,
MYSQLI_UNIQUE_KEY_FLAG,
MYSQLI_MULTIPLE_KEY_FLAG,
MYSQLI_BLOB_FLAG,
MYSQLI_UNSIGNED_FLAG,
MYSQLI_ZEROFILL_FLAG,
MYSQLI_AUTO_INCREMENT_FLAG,
MYSQLI_TIMESTAMP_FLAG,
MYSQLI_SET_FLAG,
MYSQLI_NUM_FLAG,
MYSQLI_PART_KEY_FLAG,
MYSQLI_GROUP_FLAG,
MYSQLI_ENUM_FLAG,
MYSQLI_BINARY_FLAG,
MYSQLI_NO_DEFAULT_VALUE_FLAG,
MYSQLI_ON_UPDATE_NOW_FLAG
);
$mysql_data_type_hash = array (
0 => 'real', // decimal
1 => 'int', // tiny int
2 => 'int', // smallint
3 => 'int', // int
4 => 'real', // float
5 => 'real', // double
6 => 'null', // null
7 => 'timestamp', // timestamp
8 => 'int', // bigint
9 => 'int', // mediumint
10 => 'date', // date
11 => 'time', // time
12 => 'datetime', // datetime
13 => 'year', // year
14 => 'date', // newdate
15 => 'string', // varchar
16 => 'int', // bit
246 => 'real', // newdecimal
247 => 'string', // enum
248 => 'string', // set
249 => 'blob', // tibyblob
250 => 'blob', // mediumblob
251 => 'blob', // longblob
252 => 'blob', // blob
253 => 'string', // string
254 => 'string', // string
246 => 'real' // decimal
);
// {{{ +-- public (int) mysql_affected_rows (&$c = null)
/**
* Get number of affected rows in previous MySQL operation
* @link http://php.net/manual/en/function.mysql-affected-rows.php
* @param resource $link_identifier [optional]
* @return int the number of affected rows on success, and -1 if the last query
* failed.
* </p>
* <p>
* If the last query was a DELETE query with no WHERE clause, all
* of the records will have been deleted from the table but this
* function will return zero with MySQL versions prior to 4.1.2.
* </p>
* <p>
* When using UPDATE, MySQL will not update columns where the new value is the
* same as the old value. This creates the possibility that
* <b>mysql_affected_rows</b> may not actually equal the number
* of rows matched, only the number of rows that were literally affected by
* the query.
* </p>
* <p>
* The REPLACE statement first deletes the record with the same primary key
* and then inserts the new record. This function returns the number of
* deleted records plus the number of inserted records.
* @since 4.0
* @since 5.0
*/
function mysql_affected_rows (&$c = null) {
if ( ($c = mysql_global_resource ($c, 1 - func_num_args ())) == null )
return;
return mysqli_affected_rows ($c);
}
// }}}
// {{{ +-- public (string) mysql_client_encoding (&$c = null)
/**
* Returns the name of the character set
* @link http://php.net/manual/en/function.mysql-client-encoding.php
* @param resource $link_identifier [optional]
* @return string the default character set name for the current connection.
* @since 4.3.0
* @since 5.0
*/
function mysql_client_encoding (&$c = null) {
if ( ($c = mysql_global_resource ($c, 1 - func_num_args ())) == null )
return;
return mysqli_character_set_name ($c);
}
// }}}
// {{{ +-- public (bool) mysql_close (&$c = null)
/**
* Close MySQL connection
* @link http://php.net/manual/en/function.mysql-close.php
* @param resource $link_identifier [optional]
* @return bool true on success or false on failure.
* @since 4.0
* @since 5.0
*/
function mysql_close (&$c = null) {
if ( ($c = mysql_global_resource ($c, 1 - func_num_args ())) == null )
return;
return mysqli_close ($c);
}
// }}}
// {{{ +-- public (resource) mysql_connect ($host = null, $user = null, $pass = null, $link = false, $flag = 0)
/**
* Open a connection to a MySQL Server
* @link http://php.net/manual/en/function.mysql-connect.php
* @param string $server [optional] <p>
* The MySQL server. It can also include a port number. e.g.
* "hostname:port" or a path to a local socket e.g. ":/path/to/socket" for
* the localhost.
* </p>
* <p>
* If the PHP directive
* mysql.default_host is undefined (default), then the default
* value is 'localhost:3306'. In &sqlsafemode;, this parameter is ignored
* and value 'localhost:3306' is always used.
* </p>
* @param string $username [optional] <p>
* The username. Default value is defined by mysql.default_user. In
* &sqlsafemode;, this parameter is ignored and the name of the user that
* owns the server process is used.
* </p>
* @param string $password [optional] <p>
* The password. Default value is defined by mysql.default_password. In
* &sqlsafemode;, this parameter is ignored and empty password is used.
* </p>
* @param bool $new_link [optional] <p>
* If a second call is made to <b>mysql_connect</b>
* with the same arguments, no new link will be established, but
* instead, the link identifier of the already opened link will be
* returned. The <i>new_link</i> parameter modifies this
* behavior and makes <b>mysql_connect</b> always open
* a new link, even if <b>mysql_connect</b> was called
* before with the same parameters.
* In &sqlsafemode;, this parameter is ignored.
* </p>
* @param int $client_flags [optional] <p>
* The <i>client_flags</i> parameter can be a combination
* of the following constants:
* 128 (enable LOAD DATA LOCAL handling),
* <b>MYSQL_CLIENT_SSL</b>,
* <b>MYSQL_CLIENT_COMPRESS</b>,
* <b>MYSQL_CLIENT_IGNORE_SPACE</b> or
* <b>MYSQL_CLIENT_INTERACTIVE</b>.
* Read the section about for further information.
* In &sqlsafemode;, this parameter is ignored.
* </p>
* @return resource a MySQL link identifier on success or false on failure.
* @since 4.0
* @since 5.0
*/
function mysql_connect ($host = null, $user = null, $pass = null, $link = false, $flag = 0) {
$GLOBALS['_MyConnErr_'] = null;
if ( $host === null )
$host = ___ini_get ('mysqli.default_host');
if ( $user === null )
$user = ___ini_get ('mysqli.default_user');
if ( $pass === null )
$pass = ___ini_get ('mysqli.default_pw');
$persistant = false;
$socket = null;
$port = ___ini_get ('mysqli.default_port');
if ( $host[0] === ':' ) {
$socket = substr ($host, 1);
$host = 'localhost';
} else {
if ( preg_match ('/^p:/', $host) ) {
$persistant = true;
$host = substr ($host, 2);
}
if ( preg_match ('/^([^:]+):([\d]+)$/', $host, $m) ) {
$host = $m[1];
$port = $m[2];
}
}
if ( $persistant === true )
$host = 'p:' . $host;
$c = mysqli_connect ($host, $user, $pass, '', $port, $socket);
$GLOBALS['_MyConnErr_'] = error_get_last ();
#if ( $GLOBALS['_MySQLCON_'] === null && is_myresource ($c) )
if ( is_myresource ($c) )
$GLOBALS['_MySQLCON_'] = &$c;
return $c;
}
// }}}
// {{{ +-- public (bool|mysqli_result|void) mysql_create_db ($name, $c = null)
/**
* @param $name
* @param null $c
* @return bool|mysqli_result|void
*/
function mysql_create_db ($name, $c = null) {
if ( ($c = mysql_global_resource ($c, 2 - func_num_args ())) == null )
return;
$name = trim ($name);
if ( ! $name )
return false;
return mysqli_query ($c, 'CREATE DATABASE ' . $name);
}
// }}}
// {{{ +-- public (bool|mysqli_result|void) mysql_createdb ($name, $c = null)
/**
* @param $name
* @param null $c
* @return bool|mysqli_result|void
*/
function mysql_createdb ($name, $c = null) {
return mysql_create_db ($name, $c);
}
// }}}
// {{{ +-- public (bool) mysql_data_seek ($result, $offset)
/**
* Move internal result pointer
* @link http://php.net/manual/en/function.mysql-data-seek.php
* @param resource $result
* @param int $row_number <p>
* The desired row number of the new result pointer.
* </p>
* @return bool true on success or false on failure.
* @since 4.0
* @since 5.0
*/
function mysql_data_seek ($result, $offset) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( $offset < 0 || $offset >= $result->num_rows ) {
$msg = sprintf ('Unable to jump to row %ld on MySQL result index %d', $offset, $result);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
return mysqli_data_seek ($result, $offset);
}
// }}}
// {{{ +-- public (string) mysql_db_name ($result, $row, $field = 'Database')
/**
* Retrieves database name from the call to <b>mysql_list_dbs</b>
* @link http://php.net/manual/en/function.mysql-db-name.php
* @param resource $result <p>
* The result pointer from a call to <b>mysql_list_dbs</b>.
* </p>
* @param int $row <p>
* The index into the result set.
* </p>
* @param mixed $field [optional] <p>
* The field name.
* </p>
* @return string the database name on success, and false on failure. If false
* is returned, use <b>mysql_error</b> to determine the nature
* of the error.
* @since 4.0
* @since 5.0
*/
function mysql_db_name ($result, $row, $field = 'Database') {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( $row < 0 || $row >= $result->num_rows ) {
$msg = sprintf ('Unable to jump to row %ld on MySQL result index %d', $row, $result);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
mysqli_data_seek ($result, $row);
if ( ! ($res = mysqli_fetch_object ($result)) )
return false;
return $res->{$field};
}
// }}}
// {{{ +-- public (resource) mysql_db_query ($db, $query, $c = null)
/**
* Selects a database and executes a query on it
* @link http://php.net/manual/en/function.mysql-db-query.php
* @param string $database <p>
* The name of the database that will be selected.
* </p>
* @param string $query <p>
* The MySQL query.
* </p>
* <p>
* Data inside the query should be properly escaped.
* </p>
* @param resource $link_identifier [optional]
* @return resource a positive MySQL result resource to the query result,
* or false on error. The function also returns true/false for
* INSERT/UPDATE/DELETE
* queries to indicate success/failure.
* @since 4.0
* @since 5.0
*/
function mysql_db_query ($db, $query, $c = null) {
if ( ($c = mysql_global_resource ($c, 3 - func_num_args ())) == null )
return;
$curdb = mysql_get_current_database ($c);
if ( mysqli_select_db ($c, $db) === false )
return false;
$r = mysqli_query ($c, $query);
if ($curdb !== null)
mysqli_select_db($c, $curdb);
return $r;
}
// }}}
// {{{ +-- public (bool|mysqli_result|void) mysql_drop_db ($db, $c = null)
/**
* @param $db
* @param null $c
* @return bool|mysqli_result|void
*/
function mysql_drop_db ($db, $c = null) {
if ( ($c = mysql_global_resource ($c, 2 - func_num_args ())) == null )
return;
$db = trim ($db);
if ( ! $db )
return false;
return mysqli_query ($c, sprintf ('DROP DATABASE %s', $db));
}
// }}}
// {{{ +-- public (int) mysql_errno ($c = null)
/**
* Returns the numerical value of the error message from previous MySQL operation
* @link http://php.net/manual/en/function.mysql-errno.php
* @param resource $link_identifier [optional]
* @return int the error number from the last MySQL function, or
* 0 (zero) if no error occurred.
* @since 4.0
* @since 5.0
*/
function mysql_errno ($c = null) {
if ( ($c = mysql_global_resource ($c, 1 - func_num_args (), true)) == null )
return -1;
return mysqli_errno ($c);
}
// }}}
// {{{ +-- public (string) mysql_error ($c = null)
/**
* Returns the text of the error message from previous MySQL operation
* @link http://php.net/manual/en/function.mysql-error.php
* @param resource $link_identifier [optional]
* @return string the error text from the last MySQL function, or
* '' (empty string) if no error occurred.
* @since 4.0
* @since 5.0
*/
function mysql_error ($c = null) {
$c = mysql_global_resource ($c, 1 - func_num_args (), true);
if ( ! is_myresource ($c) ) {
if ( is_array ($GLOBALS['_MyConnErr_']) && trim ($GLOBALS['_MyConnErr_']['message']) ) {
preg_match ('/[a-z_]+\(\):\s+\([^)]+\):\s+(.+)/i', $GLOBALS['_MyConnErr_']['message'], $msg);
return $msg[1];
}
return null;
}
return mysqli_error ($c);
}
// }}}
if ( ! function_exists ('mysql_escape_string') ) {
// {{{ +-- public (string) mysql_escape_string ($escape)
/**
* Escapes a string for use in a mysql_query
* @link http://php.net/manual/en/function.mysql-escape-string.php
* @param string $unescaped_string <p>
* The string that is to be escaped.
* </p>
* @return string the escaped string.
* @since 4.0.3
* @since 5.0
*/
function mysql_escape_string ($escape) {
return mysqli_real_escape_string ($GLOBALS['_MySQLCON_'], $escape);
}
// }}}
}
// {{{ +-- public (array) mysql_fetch_array ($result, $type = MYSQLI_BOTH)
/**
* Fetch a result row as an associative array, a numeric array, or both
* @link http://php.net/manual/en/function.mysql-fetch-array.php
* @param resource $result
* @param int $result_type [optional] <p>
* The type of array that is to be fetched. It's a constant and can
* take the following values: <b>MYSQL_ASSOC</b>,
* <b>MYSQL_NUM</b>, and
* <b>MYSQL_BOTH</b>.
* </p>
* @return array an array of strings that corresponds to the fetched row, or false
* if there are no more rows. The type of returned array depends on
* how <i>result_type</i> is defined. By using
* <b>MYSQL_BOTH</b> (default), you'll get an array with both
* associative and number indices. Using <b>MYSQL_ASSOC</b>, you
* only get associative indices (as <b>mysql_fetch_assoc</b>
* works), using <b>MYSQL_NUM</b>, you only get number indices
* (as <b>mysql_fetch_row</b> works).
* </p>
* <p>
* If two or more columns of the result have the same field names,
* the last column will take precedence. To access the other column(s)
* of the same name, you must use the numeric index of the column or
* make an alias for the column. For aliased columns, you cannot
* access the contents with the original column name.
* @since 4.0
* @since 5.0
*/
function mysql_fetch_array ($result, $type = MYSQLI_BOTH) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
$r = mysqli_fetch_array ($result, $type);
if ( $r === null )
$r = false;
return $r;
}
// }}}
// {{{ +-- public (array) mysql_fetch_assoc ($result)
/**
* Fetch a result row as an associative array
* @link http://php.net/manual/en/function.mysql-fetch-assoc.php
* @param resource $result
* @return array an associative array of strings that corresponds to the fetched row, or
* false if there are no more rows.
* </p>
* <p>
* If two or more columns of the result have the same field names,
* the last column will take precedence. To access the other
* column(s) of the same name, you either need to access the
* result with numeric indices by using
* <b>mysql_fetch_row</b> or add alias names.
* See the example at the <b>mysql_fetch_array</b>
* description about aliases.
* @since 4.0.3
* @since 5.0
*/
function mysql_fetch_assoc ($result) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
return mysql_fetch_array ($result, MYSQLI_ASSOC);
}
// }}}
// {{{ +-- public (object) mysql_fetch_field ($result, $offset = null)
/**
* Get column information from a result and return as an object
* @link http://php.net/manual/en/function.mysql-fetch-field.php
* @param resource $result
* @param int $field_offset [optional] <p>
* The numerical field offset. If the field offset is not specified, the
* next field that was not yet retrieved by this function is retrieved.
* The <i>field_offset</i> starts at 0.
* </p>
* @return object an object containing field information. The properties
* of the object are:
* </p>
* <p>
* name - column name
* table - name of the table the column belongs to
* def - default value of the column
* max_length - maximum length of the column
* not_null - 1 if the column cannot be null
* primary_key - 1 if the column is a primary key
* unique_key - 1 if the column is a unique key
* multiple_key - 1 if the column is a non-unique key
* numeric - 1 if the column is numeric
* blob - 1 if the column is a BLOB
* type - the type of the column
* unsigned - 1 if the column is unsigned
* zerofill - 1 if the column is zero-filled
* @since 4.0
* @since 5.0
*/
function mysql_fetch_field ($result, $offset = null) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( $offset !== null ) {
if ( $offset < 0 || $offset >= $result->field_count ) {
$msg = sprintf ('Unable to jump to field %ld on MySQL result index %d', $offset, $result);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
$res = mysqli_fetch_field_direct ($result, $offset);
} else
$res = mysqli_fetch_field ($result);
if ( $res === false )
return false;
$r = (object) array (
'name' => $res->name,
'table' => $res->table,
'def' => '', // default value
'max_length' => $res->max_length,
'not_null' => ($res->flags & MYSQLI_NOT_NULL_FLAG) ? 1 : 0,
'primary_key' => ($res->flags & MYSQLI_PRI_KEY_FLAG) ? 1 : 0,
'unique_key' => ($res->flags & MYSQLI_UNIQUE_KEY_FLAG) ? 1 : 0,
'multiple_key' => ($res->flags & MYSQLI_MULTIPLE_KEY_FLAG) ? 1 : 0,
'numeric' => ($res->flags & MYSQLI_NUM_FLAG) ? 1 : 0,
'blob' => ($res->flags & MYSQLI_BLOB_FLAG) ? 1 : 0,
'type' => $GLOBALS['mysql_data_type_hash'][$res->type],
'unsigned' => ($res->flags & MYSQLI_UNSIGNED_FLAG) ? 1 : 0,
'zerofill' => ($res->flags & MYSQLI_ZEROFILL_FLAG) ? 1 : 0
);
// exception
if ( $res->flags === 0 ) // decimal type
$r->numeric = 1;
if ( $r->type == 'timestamp' )
$r->numeric = 1;
return $r;
}
// }}}
// {{{ +-- public (array) mysql_fetch_lengths ($result)
/**
* Get the length of each output in a result
* @link http://php.net/manual/en/function.mysql-fetch-lengths.php
* @param resource $result
* @return array An array of lengths on success or false on failure.
* @since 4.0
* @since 5.0
*/
function mysql_fetch_lengths ($result) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
return mysqli_fetch_lengths ($result);
}
// }}}
// {{{ +-- public (stdClass|object) mysql_fetch_object ($result, $classname = null, $params = null)
/**
* Fetch a result row as an object
* @link http://php.net/manual/en/function.mysql-fetch-object.php
* @param resource $result
* @param string $class_name [optional] <p>
* The name of the class to instantiate, set the properties of and return.
* If not specified, a <b>stdClass</b> object is returned.
* </p>
* @param array $params [optional] <p>
* An optional array of parameters to pass to the constructor
* for <i>class_name</i> objects.
* </p>
* @return stdClass|object an object with string properties that correspond to the
* fetched row, or false if there are no more rows.
* </p>
* <p>
* mysql_fetch_row fetches one row of data from
* the result associated with the specified result identifier. The
* row is returned as an array. Each result column is stored in an
* array offset, starting at offset 0.
* @since 4.0
* @since 5.0
*/
function mysql_fetch_object ($result, $classname = null, $params = null) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( $classname !== null ) {
if ( ! class_exists ($classname) ) {
$msg = sprintf ('Class \'%s\' not found', $classname);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( $params !== null ) {
if ( ! is_array ($params) ) {
$msg = 'Argument 3 passed to mysql_fetch_object() must be of the type array';
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
$r = mysqli_fetch_object ($result, $classname, $params);
} else
$r = mysqli_fetch_object ($result, $classname);
} else
$r = mysqli_fetch_object ($result);
return ($r === null) ? false : $r;
}
// }}}
// {{{ +-- public (array) mysql_fetch_row ($result)
/**
* Get a result row as an enumerated array
* @link http://php.net/manual/en/function.mysql-fetch-row.php
* @param resource $result
* @return array an numerical array of strings that corresponds to the fetched row, or
* false if there are no more rows.
* </p>
* <p>
* <b>mysql_fetch_row</b> fetches one row of data from
* the result associated with the specified result identifier. The
* row is returned as an array. Each result column is stored in an
* array offset, starting at offset 0.
* @since 4.0
* @since 5.0
*/
function mysql_fetch_row ($result) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
$r = mysqli_fetch_row ($result);
return ($r === null ) ? false : $r;
}
// }}}
// {{{ +-- public (string) mysql_field_flags ($result, $offset)
/**
* Get the flags associated with the specified field in a result
* @link http://php.net/manual/en/function.mysql-field-flags.php
* @param resource $result
* @param int $field_offset
* @return string a string of flags associated with the result or false on failure.
* </p>
* <p>
* The following flags are reported, if your version of MySQL
* is current enough to support them: "not_null",
* "primary_key", "unique_key",
* "multiple_key", "blob",
* "unsigned", "zerofill",
* "binary", "enum",
* "auto_increment" and "timestamp".
* @since 4.0
* @since 5.0
*/
function mysql_field_flags ($result, $offset) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( $offset < 0 || $offset >= $result->field_count ) {
$msg = sprintf ('Unable to jump to field %ld on MySQL result index %d', $offset, $result);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( ($r = mysqli_fetch_field_direct ($result, $offset)) === false ) {
return false;
}
$res = false;
foreach ( $GLOBALS['msyql_filed_flags'] as $flag ) {
#printf ("#### {$r->flags} : {$flag} => %d\n", ($r->flags & $flag));
if ( ! ($r->flags & $flag) )
continue;
switch ( $flag ) {
case MYSQLI_NOT_NULL_FLAG :
$res .= 'not_null '; break;
case MYSQLI_PRI_KEY_FLAG :
$res .= 'primary_key '; break;
case MYSQLI_UNIQUE_KEY_FLAG :
$res .= 'unique_key '; break;
case MYSQLI_MULTIPLE_KEY_FLAG :
$res .= 'multiple_key '; break;
case MYSQLI_BLOB_FLAG :
$res .= 'blob '; break;
case MYSQLI_UNSIGNED_FLAG :
$res .= 'unsigned '; break;
case MYSQLI_ZEROFILL_FLAG :
$res .= 'zerofill '; break;
case MYSQLI_AUTO_INCREMENT_FLAG :
$res .= 'auto_increment '; break;
case MYSQLI_TIMESTAMP_FLAG :
$res .= 'timestamp '; break;
case MYSQLI_SET_FLAG :
$res .= 'set '; break;
//case MYSQLI_NUM_FLAG :
// $res .= 'numeric '; break;
case MYSQLI_PART_KEY_FLAG :
$res .= 'part_key '; break;
//case MYSQLI_GROUP_FLAG :
// $res .= 'group '; break;
case MYSQLI_ENUM_FLAG :
$res .= 'enum '; break;
case MYSQLI_BINARY_FLAG :
$res .= 'binary '; break;
//case MYSQLI_NO_DEFAULT_VALUE_FLAG :
// $res .= 'no_default_value '; break;
case MYSQLI_ON_UPDATE_NOW_FLAG :
$res .= 'on_update_now '; break;
}
}
return rtrim ($res);
}
// }}}
// {{{ +-- public (int) mysql_field_len ($result, $offset)
/**
* Returns the length of the specified field
* @link http://php.net/manual/en/function.mysql-field-len.php
* @param resource $result
* @param int $field_offset
* @return int The length of the specified field index on success or false on failure.
* @since 4.0
* @since 5.0
*/
function mysql_field_len ($result, $offset) {
if ( ($r = mysqli_mysqli_fetch_field_direct ($result, $offset)) === false )
return false;
return $r->length;
}
// }}}
// {{{ +-- public (string) mysql_field_name ($result, $offset)
/**
* Get the name of the specified field in a result
* @link http://php.net/manual/en/function.mysql-field-name.php
* @param resource $result
* @param int $field_offset
* @return string The name of the specified field index on success or false on failure.
* @since 4.0
* @since 5.0
*/
function mysql_field_name ($result, $offset) {
if ( ($r = mysqli_mysqli_fetch_field_direct ($result, $offset)) === false )
return false;
return $r->name;
}
// }}}
// {{{ +-- public (bool) mysql_field_seek ($result, $offset)
/**
* Set result pointer to a specified field offset
* @link http://php.net/manual/en/function.mysql-field-seek.php
* @param resource $result
* @param int $field_offset
* @return bool true on success or false on failure.
* @since 4.0
* @since 5.0
*/
function mysql_field_seek ($result, $offset) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( ! isset ($offset) ) {
$msg = sprintf ('expects parameter 2 to be offset value');
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( $offset < 0 || $offset >= mysqli_num_fields ($result) ) {
$msg = sprintf ('Unable to jump to field %ld on MySQL result index %d', $offset, $result);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
return mysqli_field_seek ($result, $offset);
}