Ticket #21663: 21663.11.diff
File 21663.11.diff, 10.7 KB (added by , 11 years ago) |
---|
-
src/wp-includes/wp-db.php
530 530 'STRICT_TRANS_TABLES', 'STRICT_ALL_TABLES', 'TRADITIONAL' ); 531 531 532 532 /** 533 * Whether to use mysqli over mysql. 534 * 535 * @since 3.9.0 536 * @access private 537 * @var bool 538 */ 539 private $use_mysqli = false; 540 541 /** 533 542 * Connects to the database server and selects a database 534 543 * 535 544 * PHP5 style constructor for compatibility with PHP5. Does … … 550 559 if ( WP_DEBUG && WP_DEBUG_DISPLAY ) 551 560 $this->show_errors(); 552 561 562 $this->use_mysqli = ( version_compare( phpversion(), '5.5', '>=' ) && function_exists( 'mysqli_connect' ) ); 563 553 564 $this->init_charset(); 554 565 555 566 $this->dbuser = $dbuser; … … 657 668 if ( ! isset( $collate ) ) 658 669 $collate = $this->collate; 659 670 if ( $this->has_cap( 'collation' ) && ! empty( $charset ) ) { 660 if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) { 661 mysql_set_charset( $charset, $dbh ); 671 if ( $this->use_mysqli ) { 672 if ( function_exists( 'mysqli_set_charset' ) && $this->has_cap( 'set_charset' ) ) { 673 mysqli_set_charset( $dbh, $charset ); 674 } else { 675 $query = $this->prepare( 'SET NAMES %s', $charset ); 676 if ( ! empty( $collate ) ) 677 $query .= $this->prepare( ' COLLATE %s', $collate ); 678 mysqli_query( $query, $dbh ); 679 } 662 680 } else { 663 $query = $this->prepare( 'SET NAMES %s', $charset ); 664 if ( ! empty( $collate ) ) 665 $query .= $this->prepare( ' COLLATE %s', $collate ); 666 mysql_query( $query, $dbh ); 681 if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) { 682 mysql_set_charset( $charset, $dbh ); 683 } else { 684 $query = $this->prepare( 'SET NAMES %s', $charset ); 685 if ( ! empty( $collate ) ) 686 $query .= $this->prepare( ' COLLATE %s', $collate ); 687 mysql_query( $query, $dbh ); 688 } 667 689 } 668 690 } 669 691 } … … 680 702 */ 681 703 function set_sql_mode( $modes = array() ) { 682 704 if ( empty( $modes ) ) { 683 $res = mysql_query( 'SELECT @@SESSION.sql_mode;', $this->dbh ); 705 if ( $this->use_mysqli ) { 706 $res = mysqli_query( $this->dbh, 'SELECT @@SESSION.sql_mode' ); 707 } else { 708 $res = mysql_query( 'SELECT @@SESSION.sql_mode', $this->dbh ); 709 } 710 684 711 if ( empty( $res ) ) { 685 712 return; 686 713 } 687 714 688 $modes_str = mysql_result( $res, 0 ); 715 if ( $this->use_mysqli ) { 716 $modes_array = mysqli_fetch_array( $res ); 717 if ( empty( $modes_array[0] ) ) { 718 return; 719 } 720 $modes_str = $modes_array[0]; 721 } else { 722 $modes_str = mysql_result( $res, 0 ); 723 } 689 724 690 725 if ( empty( $modes_str ) ) { 691 726 return; … … 715 750 716 751 $modes_str = implode( ',', $modes ); 717 752 718 mysql_query( "SET SESSION sql_mode='$modes_str';", $this->dbh ); 753 if ( $this->use_mysqli ) { 754 mysqli_query( $this->dbh, "SET SESSION sql_mode='$modes_str'" ); 755 } else { 756 mysql_query( "SET SESSION sql_mode='$modes_str'", $this->dbh ); 757 } 719 758 } 720 759 721 760 /** … … 900 939 if ( is_null($dbh) ) 901 940 $dbh = $this->dbh; 902 941 903 if ( !@mysql_select_db( $db, $dbh ) ) { 942 if ( $this->use_mysqli ) { 943 $success = @mysqli_select_db( $dbh, $db ); 944 } else { 945 $success = @mysql_select_db( $db, $dbh ); 946 } 947 if ( ! $success ) { 904 948 $this->ready = false; 905 949 wp_load_translations_early(); 906 950 $this->bail( sprintf( __( '<h1>Can’t select database</h1> … … 936 980 } 937 981 938 982 /** 939 * Real escape, using mysql _real_escape_string()983 * Real escape, using mysqli_real_escape_string() or mysql_real_escape_string() 940 984 * 985 * @see mysqli_real_escape_string() 941 986 * @see mysql_real_escape_string() 942 987 * @since 2.8.0 943 988 * @access private … … 946 991 * @return string escaped 947 992 */ 948 993 function _real_escape( $string ) { 949 if ( $this->dbh ) 950 return mysql_real_escape_string( $string, $this->dbh ); 994 if ( $this->dbh ) { 995 if ( $this->use_mysqli ) { 996 return mysqli_real_escape_string( $this->dbh, $string ); 997 } else { 998 return mysql_real_escape_string( $string, $this->dbh ); 999 } 1000 } 951 1001 952 1002 $class = get_class( $this ); 953 1003 _doing_it_wrong( $class, "$class must set a database connection for use with escaping.", E_USER_NOTICE ); … … 1093 1143 function print_error( $str = '' ) { 1094 1144 global $EZSQL_ERROR; 1095 1145 1096 if ( !$str ) 1097 $str = mysql_error( $this->dbh ); 1146 if ( !$str ) { 1147 if ( $this->use_mysqli ) { 1148 $str = mysqli_error( $this->dbh ); 1149 } else { 1150 $str = mysql_error( $this->dbh ); 1151 } 1152 } 1098 1153 $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str ); 1099 1154 1100 1155 if ( $this->suppress_errors ) … … 1197 1252 $this->rows_affected = $this->num_rows = 0; 1198 1253 $this->last_error = ''; 1199 1254 1200 if ( is_resource( $this->result ) ) 1201 mysql_free_result( $this->result ); 1255 if ( is_resource( $this->result ) ) { 1256 if ( $this->use_mysqli ) { 1257 mysqli_free_result( $this->result ); 1258 } else { 1259 mysql_free_result( $this->result ); 1260 } 1261 } 1202 1262 } 1203 1263 1204 1264 /** … … 1219 1279 $new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true; 1220 1280 $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0; 1221 1281 1222 if ( WP_DEBUG ) { 1223 $error_reporting = false; 1224 if ( defined( 'E_DEPRECATED' ) ) { 1225 $error_reporting = error_reporting(); 1226 error_reporting( $error_reporting ^ E_DEPRECATED ); 1282 if ( $this->use_mysqli ) { 1283 $this->dbh = mysqli_init(); 1284 if ( WP_DEBUG ) { 1285 mysqli_real_connect( $this->dbh, $this->dbhost, $this->dbuser, $this->dbpassword, null, null, null, $client_flags ); 1286 } else { 1287 @mysqli_real_connect( $this->dbh, $this->dbhost, $this->dbuser, $this->dbpassword, null, null, null, $client_flags ); 1227 1288 } 1228 $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1229 if ( false !== $error_reporting ) { 1230 error_reporting( $error_reporting ); 1289 } else { 1290 if ( WP_DEBUG ) { 1291 $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1292 } else { 1293 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1231 1294 } 1232 } else {1233 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );1234 1295 } 1235 1296 1236 1297 if ( ! $this->dbh && $allow_bail ) { … … 1276 1337 * @return bool True if the connection is up. 1277 1338 */ 1278 1339 function check_connection() { 1279 if ( @mysql_ping( $this->dbh ) ) { 1280 return true; 1340 if ( $this->use_mysqli ) { 1341 if ( @mysqli_ping( $this->dbh ) ) { 1342 return true; 1343 } 1344 } else { 1345 if ( @mysql_ping( $this->dbh ) ) { 1346 return true; 1347 } 1281 1348 } 1282 1349 1283 1350 $error_reporting = false; … … 1359 1426 $this->_do_query( $query ); 1360 1427 1361 1428 // MySQL server has gone away, try to reconnect 1362 if ( empty( $this->dbh ) || 2006 == mysql_errno( $this->dbh ) ) { 1429 $mysql_errno = 0; 1430 if ( ! empty( $this->dbh ) ) { 1431 if ( $this->use_mysqli ) { 1432 $mysql_errno = mysqli_errno( $this->dbh ); 1433 } else { 1434 $mysql_errno = mysql_errno( $this->dbh ); 1435 } 1436 } 1437 1438 if ( empty( $this->dbh ) || 2006 == $mysql_errno ) { 1363 1439 if ( $this->check_connection() ) { 1364 1440 $this->_do_query( $query ); 1365 1441 } 1366 1442 } 1367 1443 1368 1444 // If there is an error then take note of it.. 1369 if ( $this->last_error = mysql_error( $this->dbh ) ) { 1445 if ( $this->use_mysqli ) { 1446 $this->last_error = mysqli_error( $this->dbh ); 1447 } else { 1448 $this->last_error = mysql_error( $this->dbh ); 1449 } 1450 1451 if ( $this->last_error ) { 1370 1452 // Clear insert_id on a subsequent failed insert. 1371 1453 if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) ) 1372 1454 $this->insert_id = 0; … … 1378 1460 if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) { 1379 1461 $return_val = $this->result; 1380 1462 } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) { 1381 $this->rows_affected = mysql_affected_rows( $this->dbh ); 1463 if ( $this->use_mysqli ) { 1464 $this->rows_affected = mysqli_affected_rows( $this->dbh ); 1465 } else { 1466 $this->rows_affected = mysql_affected_rows( $this->dbh ); 1467 } 1382 1468 // Take note of the insert_id 1383 1469 if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) { 1384 $this->insert_id = mysql_insert_id($this->dbh); 1470 if ( $this->use_mysqli ) { 1471 $this->insert_id = mysqli_insert_id( $this->dbh ); 1472 } else { 1473 $this->insert_id = mysql_insert_id( $this->dbh ); 1474 } 1385 1475 } 1386 1476 // Return number of rows affected 1387 1477 $return_val = $this->rows_affected; 1388 1478 } else { 1389 1479 $num_rows = 0; 1390 while ( $row = @mysql_fetch_object( $this->result ) ) { 1391 $this->last_result[$num_rows] = $row; 1392 $num_rows++; 1480 if ( $this->use_mysqli ) { 1481 while ( $row = @mysqli_fetch_object( $this->result ) ) { 1482 $this->last_result[$num_rows] = $row; 1483 $num_rows++; 1484 } 1485 } else { 1486 while ( $row = @mysql_fetch_object( $this->result ) ) { 1487 $this->last_result[$num_rows] = $row; 1488 $num_rows++; 1489 } 1393 1490 } 1394 1491 1395 1492 // Log number of rows the query returned … … 1416 1513 $this->timer_start(); 1417 1514 } 1418 1515 1419 $this->result = @mysql_query( $query, $this->dbh ); 1516 if ( $this->use_mysqli ) { 1517 $this->result = @mysqli_query( $this->dbh, $query ); 1518 } else { 1519 $this->result = @mysql_query( $query, $this->dbh ); 1520 } 1420 1521 $this->num_queries++; 1421 1522 1422 1523 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) { … … 1754 1855 if ( $this->col_info ) 1755 1856 return; 1756 1857 1757 for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) { 1758 $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i ); 1858 if ( $this->use_mysqli ) { 1859 for ( $i = 0; $i < @mysqli_num_fields( $this->result ); $i++ ) { 1860 $this->col_info[ $i ] = @mysqli_fetch_field( $this->result, $i ); 1861 } 1862 } else { 1863 for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) { 1864 $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i ); 1865 } 1759 1866 } 1760 1867 } 1761 1868 … … 1927 2034 * @return false|string false on failure, version number on success 1928 2035 */ 1929 2036 function db_version() { 1930 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) ); 2037 if ( $this->use_mysqli ) { 2038 $server_info = mysqli_get_server_info( $this->dbh ); 2039 } else { 2040 $server_info = mysql_get_server_info( $this->dbh ); 2041 } 2042 return preg_replace( '/[^0-9.].*/', '', $server_info ); 1931 2043 } 1932 2044 } -
tests/phpunit/tests/db.php
50 50 $var = $wpdb->get_var( "SELECT ID FROM $wpdb->users LIMIT 1" ); 51 51 $this->assertGreaterThan( 0, $var ); 52 52 53 mysql_close( $wpdb->dbh ); 53 if ( $wpdb->use_mysqli ) { 54 mysqli_close( $wpdb->dbh ); 55 } else { 56 mysql_close( $wpdb->dbh ); 57 } 54 58 unset( $wpdb->dbh ); 55 59 56 60 $var = $wpdb->get_var( "SELECT ID FROM $wpdb->users LIMIT 1" );