├── LICENSE ├── README.md └── mysql.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 e-sites 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | MySQL wrapper for MySQLi 2 | ======================== 3 | 4 | This collection of MySQL functions is trying to be a drop in replacement for the PHP MySQL extension. In PHP 5.5 the MySQL extension is deprectated and triggers errors. 5 | This package is trying to support legacy projects without the need of having the MySQL extension installed by using this package you are not hold back to update to new PHP versions. 6 | 7 | The return values of my functions are aimed to match the native MySQL functions as close as possible. -------------------------------------------------------------------------------- /mysql.php: -------------------------------------------------------------------------------- 1 | select_db($databaseName); 90 | } 91 | 92 | /** 93 | * @param $query 94 | * @param mysqli $mysqli 95 | * @return bool|mysqli_result 96 | */ 97 | function mysql_query($query, mysqli $mysqli = null) 98 | { 99 | return getLinkIdentifier($mysqli)->query($query); 100 | } 101 | 102 | /** 103 | * @param $string 104 | * @param mysqli $mysqli 105 | * @return string 106 | */ 107 | function mysql_real_escape_string($string, mysqli $mysqli = null) 108 | { 109 | return getLinkIdentifier($mysqli)->escape_string($string); 110 | } 111 | 112 | /** 113 | * @param $string 114 | * @return string 115 | */ 116 | function mysql_escape_string($string) 117 | { 118 | return mysql_real_escape_string($string); 119 | } 120 | /** 121 | * @param mysqli_result $result 122 | * @return bool|array 123 | */ 124 | function mysql_fetch_assoc(mysqli_result $result) 125 | { 126 | $result = $result->fetch_assoc(); 127 | if ($result === NULL) { 128 | $result = false; 129 | } 130 | 131 | return $result; 132 | } 133 | 134 | /** 135 | * @param mysqli_result $result 136 | * @return object|stdClass 137 | */ 138 | function mysql_fetch_object(mysqli_result $result) 139 | { 140 | $result = $result->fetch_object(); 141 | if ($result === NULL) { 142 | $result = false; 143 | } 144 | 145 | return $result; 146 | } 147 | 148 | /** 149 | * @param mysqli_result $result 150 | * @return bool|int 151 | */ 152 | function mysql_num_rows(mysqli_result $result) 153 | { 154 | $result = $result->num_rows; 155 | if ($result === NULL) { 156 | $result = false; 157 | } 158 | 159 | return $result; 160 | } 161 | 162 | /** 163 | * @param mysqli_result $result 164 | * @return bool|array 165 | */ 166 | function mysql_fetch_row(mysqli_result $result) 167 | { 168 | $result = $result->fetch_row(); 169 | if ($result === NULL) { 170 | $result = false; 171 | } 172 | 173 | return $result; 174 | } 175 | 176 | /** 177 | * @param mysqli $mysqli 178 | * @return int 179 | */ 180 | function mysql_affected_rows(mysqli $mysqli = null) 181 | { 182 | return mysqli_affected_rows(getLinkIdentifier($mysqli)); 183 | } 184 | 185 | /** 186 | * @return void 187 | */ 188 | function mysql_client_encoding(mysqli $mysqli = null) 189 | { 190 | return mysqli_character_set_name(getLinkIdentifier($mysqli)); 191 | } 192 | 193 | /** 194 | * @param mysqli $mysqli 195 | * @return bool 196 | */ 197 | function mysql_close(mysqli $mysqli = null) 198 | { 199 | return mysqli_close(getLinkIdentifier($mysqli)); 200 | } 201 | 202 | /** 203 | * @return bool 204 | */ 205 | function mysql_create_db($database_name, mysqli $mysqli = null) 206 | { 207 | trigger_error('This function was deprecated in PHP 4.3.0 and is therefor not supported', E_USER_DEPRECATED); 208 | return false; 209 | } 210 | 211 | /** 212 | * @param mysqli $mysqli 213 | * @return int 214 | */ 215 | function mysql_errno(mysqli $mysqli = null) 216 | { 217 | return mysqli_errno(getLinkIdentifier($mysqli)); 218 | } 219 | 220 | /** 221 | * Adjusts the result pointer to an arbitrary row in the result 222 | * 223 | * @param $result 224 | * @param $row 225 | * @param int $field 226 | * @return bool 227 | */ 228 | function mysql_db_name(mysqli_result $result, $row, $field=null) 229 | { 230 | mysqli_data_seek($result,$row); 231 | $f = mysqli_fetch_row($result); 232 | 233 | return $f[0]; 234 | } 235 | 236 | /** 237 | * @param mysqli $mysqli 238 | * @return string 239 | */ 240 | function mysql_error(mysqli $mysqli = null) 241 | { 242 | return mysqli_error(getLinkIdentifier($mysqli)); 243 | } 244 | 245 | /** 246 | * @param mysqli_result $result 247 | * @param $result_type 248 | * @return void 249 | */ 250 | function mysql_fetch_array(mysqli_result $result, $result_type = MYSQL_BOTH) 251 | { 252 | return mysqli_fetch_array($result, $result_type); 253 | } 254 | 255 | /** 256 | * @param mysqli $mysqli 257 | * @return bool 258 | */ 259 | function mysql_ping(mysqli $mysqli = null) 260 | { 261 | return mysqli_ping(getLinkIdentifier($mysqli)); 262 | } 263 | 264 | /** 265 | * @param $query 266 | * @param mysqli $mysqli 267 | */ 268 | function mysql_unbuffered_query($query, mysqli $mysqli = null) 269 | { 270 | return mysqli_query(getLinkIdentifier($mysqli), $query, MYSQLI_USE_RESULT); 271 | } 272 | 273 | /** 274 | * @return string 275 | */ 276 | function mysql_get_client_info() 277 | { 278 | global $__MYSQLI_WRAPPER_LINK; 279 | // Better use the reference to current connection 280 | return mysqli_get_client_info($__MYSQLI_WRAPPER_LINK); 281 | } 282 | 283 | /** 284 | * @param mysqli_result $result 285 | * @return void 286 | */ 287 | function mysql_free_result(mysqli_result $result) 288 | { 289 | // mysqli_free_result have a void return 290 | mysqli_free_result($result); 291 | return TRUE; 292 | } 293 | 294 | /** 295 | * @param mysqli $mysqli 296 | * @return bool|mysqli_result 297 | */ 298 | function mysql_list_dbs(mysqli $mysqli = null) 299 | { 300 | 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); 301 | 302 | return mysqli_query(getLinkIdentifier($mysqli), 'SHOW DATABASES'); 303 | } 304 | 305 | /** 306 | * @param $database_name 307 | * @param $table_name 308 | * @param null $mysqli 309 | * @return bool|mysqli_result 310 | */ 311 | function mysql_list_fields($database_name, $table_name, mysqli $mysqli = null) 312 | { 313 | 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); 314 | 315 | $mysqli = getLinkIdentifier($mysqli); 316 | $db = mysqli_escape_string($mysqli, $database_name); 317 | $table = mysqli_escape_string($mysqli, $table_name); 318 | 319 | return mysqli_query($mysqli, sprintf('SHOW COLUMNS FROM %s.%s', $db, $table)); 320 | } 321 | 322 | /** 323 | * @param mysqli $mysqli 324 | * @return bool|mysqli_result 325 | */ 326 | function mysql_list_processes(mysqli $mysqli = null) 327 | { 328 | return mysqli_query(getLinkIdentifier($mysqli), 'SHOW PROCESSLIST'); 329 | } 330 | 331 | /** 332 | * @param $charset 333 | * @param null $mysqli 334 | * @return bool 335 | */ 336 | function mysql_set_charset($charset, mysqli $mysqli = null) 337 | { 338 | return mysqli_set_charset(getLinkIdentifier($mysqli), $charset); 339 | } 340 | 341 | /** 342 | * @param null $mysqli 343 | * @return bool|string 344 | */ 345 | function mysql_info(mysqli $mysqli = null) 346 | { 347 | $result = mysqli_info(getLinkIdentifier($mysqli)); 348 | if ($result === NULL) { 349 | $result = false; 350 | } 351 | 352 | return $result; 353 | } 354 | 355 | /** 356 | * Get current system status 357 | * 358 | * @param null $mysqli 359 | * @return bool|string 360 | */ 361 | function mysql_stat(mysqli $mysqli = null) 362 | { 363 | return mysqli_stat(getLinkIdentifier($mysqli)); 364 | } 365 | 366 | /** 367 | * Return the current thread ID 368 | * 369 | * @param null $mysqli 370 | * @return bool|string 371 | */ 372 | function mysql_thread_id(mysqli $mysqli = null) 373 | { 374 | return mysqli_thread_id(getLinkIdentifier($mysqli)); 375 | } 376 | 377 | /** 378 | * Get MySQL host info 379 | * 380 | * @param null $mysqli 381 | * @return bool|string 382 | */ 383 | function mysql_get_host_info(mysqli $mysqli = null) 384 | { 385 | return mysqli_get_host_info(getLinkIdentifier($mysqli)); 386 | } 387 | 388 | /** 389 | * Get MySQL protocol info 390 | * 391 | * @param null $mysqli 392 | * @return bool|string 393 | */ 394 | function mysql_get_proto_info(mysqli $mysqli = null) 395 | { 396 | return mysqli_get_proto_info(getLinkIdentifier($mysqli)); 397 | } 398 | 399 | /** 400 | * Get MySQL server info 401 | * 402 | * @param null $mysqli 403 | * @return bool|string 404 | */ 405 | function mysql_get_server_info(mysqli $mysqli = null) 406 | { 407 | return mysqli_get_server_info(getLinkIdentifier($mysqli)); 408 | } 409 | 410 | /** 411 | * Get table name of field 412 | * 413 | * @param $result 414 | * @param $row 415 | * @return bool 416 | */ 417 | function mysql_tablename(mysqli_result $result, $row) 418 | { 419 | mysqli_data_seek($result, $row); 420 | $f = mysqli_fetch_array($result); 421 | 422 | return $f[0]; 423 | } 424 | 425 | /** 426 | * Get the ID generated in the last query 427 | * 428 | * @param null $mysqli 429 | * @return int|string 430 | */ 431 | function mysql_insert_id(mysqli $mysqli = null) 432 | { 433 | return mysqli_insert_id(getLinkIdentifier($mysqli)); 434 | } 435 | 436 | /** 437 | * Get result data 438 | * 439 | * @param $result 440 | * @param $row 441 | * @param int $field 442 | * @return mixed 443 | */ 444 | function mysql_result($result, $row, $field = 0) 445 | { 446 | $result->data_seek($row); 447 | $row = $result->fetch_array(); 448 | if (!isset($row[$field])) { 449 | return false; 450 | } 451 | 452 | return $row[$field]; 453 | } 454 | 455 | /** 456 | * Get number of fields in result 457 | * 458 | * @param mysqli_result $result 459 | * @return int 460 | */ 461 | function mysql_num_fields(mysqli_result $result) 462 | { 463 | return mysqli_num_fields($result); 464 | } 465 | 466 | /** 467 | * List tables in a MySQL database 468 | * 469 | * @param null $mysqli 470 | * @return bool|string 471 | */ 472 | function mysql_list_tables($database_name, mysqli $mysqli = null) 473 | { 474 | 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); 475 | 476 | $mysqli = getLinkIdentifier($mysqli); 477 | $db = mysqli_escape_string($mysqli, $database_name); 478 | 479 | return mysqli_query($mysqli, sprintf('SHOW TABLES FROM %s', $db)); 480 | } 481 | 482 | /** 483 | * Get column information from a result and return as an object 484 | * 485 | * @param mysqli_result $result 486 | * @param int $field_offset 487 | * @return bool|object 488 | */ 489 | function mysql_fetch_field(mysqli_result $result, $field_offset = 0) 490 | { 491 | if ($field_offset) { 492 | mysqli_field_seek($result, $field_offset); 493 | } 494 | 495 | return mysqli_fetch_field($result); 496 | } 497 | 498 | /** 499 | * Returns the length of the specified field 500 | * 501 | * @param mysqli_result $result 502 | * @param int $field_offset 503 | * @return bool 504 | */ 505 | function mysql_field_len(mysqli_result $result, $field_offset = 0) 506 | { 507 | $fieldInfo = mysqli_fetch_field_direct($result, $field_offset); 508 | return $fieldInfo->length; 509 | } 510 | 511 | /** 512 | * @return bool 513 | */ 514 | function mysql_drop_db() 515 | { 516 | trigger_error('This function is deprecated since PHP 4.3.0 and therefore not implemented', E_USER_DEPRECATED); 517 | return false; 518 | } 519 | 520 | /** 521 | * Move internal result pointer 522 | * 523 | * @param mysqli_result $result 524 | * @param int $row_number 525 | * @return void 526 | */ 527 | function mysql_data_seek(mysqli_result $result, $row_number = 0) 528 | { 529 | return mysqli_data_seek($result, $row_number); 530 | } 531 | 532 | /** 533 | * Get the name of the specified field in a result 534 | * 535 | * @param $result 536 | * @param $field_offset 537 | * @return bool 538 | */ 539 | function mysql_field_name($result, $field_offset = 0) 540 | { 541 | $props = mysqli_fetch_field_direct($result, $field_offset); 542 | return is_object($props) ? $props->name : false; 543 | } 544 | 545 | /** 546 | * Get the length of each output in a result 547 | * 548 | * @param mysqli_result $result 549 | * @return array|bool 550 | */ 551 | function mysql_fetch_lengths(mysqli_result $result) 552 | { 553 | return mysqli_fetch_lengths($result); 554 | } 555 | 556 | /** 557 | * Get the type of the specified field in a result 558 | * @param mysqli_result $result 559 | * @param $field_offset 560 | * @return string 561 | */ 562 | function mysql_field_type(mysqli_result $result, $field_offset = 0) 563 | { 564 | $unknown = 'unknown'; 565 | $info = mysqli_fetch_field_direct($result, $field_offset); 566 | if (empty($info->type)) { 567 | return $unknown; 568 | } 569 | 570 | switch ($info->type) { 571 | case MYSQLI_TYPE_FLOAT: 572 | case MYSQLI_TYPE_DOUBLE: 573 | case MYSQLI_TYPE_DECIMAL: 574 | case MYSQLI_TYPE_NEWDECIMAL: 575 | return 'real'; 576 | 577 | case MYSQLI_TYPE_BIT: 578 | return 'bit'; 579 | 580 | case MYSQLI_TYPE_TINY: 581 | return 'tinyint'; 582 | 583 | case MYSQLI_TYPE_TIME: 584 | return 'time'; 585 | 586 | case MYSQLI_TYPE_DATE: 587 | return 'date'; 588 | 589 | case MYSQLI_TYPE_DATETIME: 590 | return 'datetime'; 591 | 592 | case MYSQLI_TYPE_TIMESTAMP: 593 | return 'timestamp'; 594 | 595 | case MYSQLI_TYPE_YEAR: 596 | return 'year'; 597 | 598 | case MYSQLI_TYPE_STRING: 599 | case MYSQLI_TYPE_VAR_STRING: 600 | return 'string'; 601 | 602 | case MYSQLI_TYPE_SHORT: 603 | case MYSQLI_TYPE_LONG: 604 | case MYSQLI_TYPE_LONGLONG: 605 | case MYSQLI_TYPE_INT24: 606 | return 'int'; 607 | 608 | case MYSQLI_TYPE_CHAR: 609 | return 'char'; 610 | 611 | case MYSQLI_TYPE_ENUM: 612 | return 'enum'; 613 | 614 | case MYSQLI_TYPE_TINY_BLOB: 615 | case MYSQLI_TYPE_MEDIUM_BLOB: 616 | case MYSQLI_TYPE_LONG_BLOB: 617 | case MYSQLI_TYPE_BLOB: 618 | return 'blob'; 619 | 620 | case MYSQLI_TYPE_NULL: 621 | return 'null'; 622 | 623 | case MYSQLI_TYPE_NEWDATE: 624 | case MYSQLI_TYPE_INTERVAL: 625 | case MYSQLI_TYPE_SET: 626 | case MYSQLI_TYPE_GEOMETRY: 627 | default: 628 | return $unknown; 629 | } 630 | } 631 | 632 | /** 633 | * Get name of the table the specified field is in 634 | * 635 | * @param mysqli_result $result 636 | * @param $field_offset 637 | * @return bool 638 | */ 639 | function mysql_field_table(mysqli_result $result, $field_offset = 0) 640 | { 641 | $info = mysqli_fetch_field_direct($result, $field_offset); 642 | if (empty($info->table)) { 643 | return false; 644 | } 645 | 646 | return $info->table; 647 | } 648 | 649 | /** 650 | * Get the flags associated with the specified field in a result 651 | * 652 | * credit to Dave Smith from phpclasses.org, andre at koethur dot de from php.net and NinjaKC from stackoverflow.com 653 | * 654 | * @param mysqli_result $result 655 | * @param int $field_offset 656 | * @return bool 657 | */ 658 | function mysql_field_flags(mysqli_result $result , $field_offset = 0) 659 | { 660 | $flags_num = mysqli_fetch_field_direct($result,$field_offset)->flags; 661 | 662 | if (!isset($flags)) 663 | { 664 | $flags = array(); 665 | $constants = get_defined_constants(true); 666 | foreach ($constants['mysqli'] as $c => $n) if (preg_match('/MYSQLI_(.*)_FLAG$/', $c, $m)) if (!array_key_exists($n, $flags)) $flags[$n] = $m[1]; 667 | } 668 | 669 | $result = array(); 670 | foreach ($flags as $n => $t) if ($flags_num & $n) $result[] = $t; 671 | 672 | $return = implode(' ', $result); 673 | $return = str_replace('PRI_KEY','PRIMARY_KEY',$return); 674 | $return = strtolower($return); 675 | 676 | return $return; 677 | } 678 | 679 | /** 680 | * Set result pointer to a specified field offset 681 | * 682 | * @param mysqli_result $result 683 | * @param int $field_offset 684 | * @return bool 685 | */ 686 | function mysql_field_seek(mysqli_result $result, $field_offset = 0) 687 | { 688 | return mysqli_field_seek($result, $field_offset); 689 | } 690 | 691 | /** 692 | * Selects a database and executes a query on it 693 | * 694 | * @param $database 695 | * @param $query 696 | * @param mysqli $mysqli 697 | * @return bool 698 | */ 699 | function mysql_db_query($database, $query, mysqli $mysqli = null) 700 | { 701 | if(mysql_select_db($database, $mysqli)) 702 | { 703 | return mysql_query($query, $mysqli); 704 | } 705 | 706 | return false; 707 | } 708 | 709 | } 710 | ?> 711 | --------------------------------------------------------------------------------