Changeset 27250
- Timestamp:
- 02/25/2014 12:39:28 AM (11 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/wp-db.php
r27249 r27250 540 540 541 541 /** 542 * Whether to use mysqli over mysql. 543 * 544 * @since 3.9.0 545 * @access private 546 * @var bool 547 */ 548 private $use_mysqli = false; 549 550 /** 542 551 * Connects to the database server and selects a database 543 552 * … … 559 568 if ( WP_DEBUG && WP_DEBUG_DISPLAY ) 560 569 $this->show_errors(); 570 571 $this->use_mysqli = ( version_compare( phpversion(), '5.5', '>=' ) && function_exists( 'mysqli_connect' ) ); 561 572 562 573 $this->init_charset(); … … 667 678 $collate = $this->collate; 668 679 if ( $this->has_cap( 'collation' ) && ! empty( $charset ) ) { 669 if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) { 670 mysql_set_charset( $charset, $dbh ); 680 if ( $this->use_mysqli ) { 681 if ( function_exists( 'mysqli_set_charset' ) && $this->has_cap( 'set_charset' ) ) { 682 mysqli_set_charset( $dbh, $charset ); 683 } else { 684 $query = $this->prepare( 'SET NAMES %s', $charset ); 685 if ( ! empty( $collate ) ) 686 $query .= $this->prepare( ' COLLATE %s', $collate ); 687 mysqli_query( $query, $dbh ); 688 } 671 689 } else { 672 $query = $this->prepare( 'SET NAMES %s', $charset ); 673 if ( ! empty( $collate ) ) 674 $query .= $this->prepare( ' COLLATE %s', $collate ); 675 mysql_query( $query, $dbh ); 690 if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) { 691 mysql_set_charset( $charset, $dbh ); 692 } else { 693 $query = $this->prepare( 'SET NAMES %s', $charset ); 694 if ( ! empty( $collate ) ) 695 $query .= $this->prepare( ' COLLATE %s', $collate ); 696 mysql_query( $query, $dbh ); 697 } 676 698 } 677 699 } … … 690 712 function set_sql_mode( $modes = array() ) { 691 713 if ( empty( $modes ) ) { 692 $res = mysql_query( 'SELECT @@SESSION.sql_mode;', $this->dbh ); 714 if ( $this->use_mysqli ) { 715 $res = mysqli_query( $this->dbh, 'SELECT @@SESSION.sql_mode' ); 716 } else { 717 $res = mysql_query( 'SELECT @@SESSION.sql_mode', $this->dbh ); 718 } 719 693 720 if ( empty( $res ) ) { 694 721 return; 695 722 } 696 723 697 $modes_str = mysql_result( $res, 0 ); 724 if ( $this->use_mysqli ) { 725 $modes_array = mysqli_fetch_array( $res ); 726 if ( empty( $modes_array[0] ) ) { 727 return; 728 } 729 $modes_str = $modes_array[0]; 730 } else { 731 $modes_str = mysql_result( $res, 0 ); 732 } 698 733 699 734 if ( empty( $modes_str ) ) { … … 725 760 $modes_str = implode( ',', $modes ); 726 761 727 mysql_query( "SET SESSION sql_mode='$modes_str';", $this->dbh ); 762 if ( $this->use_mysqli ) { 763 mysqli_query( $this->dbh, "SET SESSION sql_mode='$modes_str'" ); 764 } else { 765 mysql_query( "SET SESSION sql_mode='$modes_str'", $this->dbh ); 766 } 728 767 } 729 768 … … 910 949 $dbh = $this->dbh; 911 950 912 if ( !@mysql_select_db( $db, $dbh ) ) { 951 if ( $this->use_mysqli ) { 952 $success = @mysqli_select_db( $dbh, $db ); 953 } else { 954 $success = @mysql_select_db( $db, $dbh ); 955 } 956 if ( ! $success ) { 913 957 $this->ready = false; 914 958 wp_load_translations_early(); … … 946 990 947 991 /** 948 * Real escape, using mysql_real_escape_string() 949 * 992 * Real escape, using mysqli_real_escape_string() or mysql_real_escape_string() 993 * 994 * @see mysqli_real_escape_string() 950 995 * @see mysql_real_escape_string() 951 996 * @since 2.8.0 … … 956 1001 */ 957 1002 function _real_escape( $string ) { 958 if ( $this->dbh ) 959 return mysql_real_escape_string( $string, $this->dbh ); 1003 if ( $this->dbh ) { 1004 if ( $this->use_mysqli ) { 1005 return mysqli_real_escape_string( $this->dbh, $string ); 1006 } else { 1007 return mysql_real_escape_string( $string, $this->dbh ); 1008 } 1009 } 960 1010 961 1011 $class = get_class( $this ); … … 1103 1153 global $EZSQL_ERROR; 1104 1154 1105 if ( !$str ) 1106 $str = mysql_error( $this->dbh ); 1155 if ( !$str ) { 1156 if ( $this->use_mysqli ) { 1157 $str = mysqli_error( $this->dbh ); 1158 } else { 1159 $str = mysql_error( $this->dbh ); 1160 } 1161 } 1107 1162 $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str ); 1108 1163 … … 1207 1262 $this->last_error = ''; 1208 1263 1209 if ( is_resource( $this->result ) ) 1210 mysql_free_result( $this->result ); 1264 if ( is_resource( $this->result ) ) { 1265 if ( $this->use_mysqli ) { 1266 mysqli_free_result( $this->result ); 1267 } else { 1268 mysql_free_result( $this->result ); 1269 } 1270 } 1211 1271 } 1212 1272 … … 1229 1289 $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0; 1230 1290 1231 if ( WP_DEBUG ) { 1232 $error_reporting = false; 1233 if ( defined( 'E_DEPRECATED' ) ) { 1234 $error_reporting = error_reporting(); 1235 error_reporting( $error_reporting ^ E_DEPRECATED ); 1236 } 1237 $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1238 if ( false !== $error_reporting ) { 1239 error_reporting( $error_reporting ); 1291 if ( $this->use_mysqli ) { 1292 $this->dbh = mysqli_init(); 1293 1294 // mysqli_real_connect doesn't support the host param including a port or socket 1295 // like mysql_connect does. This duplicates how mysql_connect detects a port and/or socket file. 1296 $port = null; 1297 $socket = null; 1298 $host = $this->dbhost; 1299 $port_or_socket = strstr( $host, ':' ); 1300 if ( ! empty( $port_or_socket ) ) { 1301 $host = strstr( $host, ':', true ); 1302 $port_or_socket = substr( $port_or_socket, 1 ); 1303 if ( 0 !== strpos( $port_or_socket, '/' ) ) { 1304 $port = intval( $port_or_socket ); 1305 $maybe_socket = strstr( $port_or_socket, ':' ); 1306 if ( ! empty( $maybe_socket ) ) { 1307 $socket = substr( $maybe_socket, 1 ); 1308 } 1309 } else { 1310 $socket = $port_or_socket; 1311 } 1312 } 1313 1314 if ( WP_DEBUG ) { 1315 mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags ); 1316 } else { 1317 @mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags ); 1240 1318 } 1241 1319 } else { 1242 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1320 if ( WP_DEBUG ) { 1321 $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1322 } else { 1323 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1324 } 1243 1325 } 1244 1326 … … 1286 1368 */ 1287 1369 function check_connection() { 1288 if ( @mysql_ping( $this->dbh ) ) { 1289 return true; 1370 if ( $this->use_mysqli ) { 1371 if ( @mysqli_ping( $this->dbh ) ) { 1372 return true; 1373 } 1374 } else { 1375 if ( @mysql_ping( $this->dbh ) ) { 1376 return true; 1377 } 1290 1378 } 1291 1379 … … 1369 1457 1370 1458 // MySQL server has gone away, try to reconnect 1371 if ( empty( $this->dbh ) || 2006 == mysql_errno( $this->dbh ) ) { 1459 $mysql_errno = 0; 1460 if ( ! empty( $this->dbh ) ) { 1461 if ( $this->use_mysqli ) { 1462 $mysql_errno = mysqli_errno( $this->dbh ); 1463 } else { 1464 $mysql_errno = mysql_errno( $this->dbh ); 1465 } 1466 } 1467 1468 if ( empty( $this->dbh ) || 2006 == $mysql_errno ) { 1372 1469 if ( $this->check_connection() ) { 1373 1470 $this->_do_query( $query ); … … 1376 1473 1377 1474 // If there is an error then take note of it.. 1378 if ( $this->last_error = mysql_error( $this->dbh ) ) { 1475 if ( $this->use_mysqli ) { 1476 $this->last_error = mysqli_error( $this->dbh ); 1477 } else { 1478 $this->last_error = mysql_error( $this->dbh ); 1479 } 1480 1481 if ( $this->last_error ) { 1379 1482 // Clear insert_id on a subsequent failed insert. 1380 1483 if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) ) … … 1388 1491 $return_val = $this->result; 1389 1492 } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) { 1390 $this->rows_affected = mysql_affected_rows( $this->dbh ); 1493 if ( $this->use_mysqli ) { 1494 $this->rows_affected = mysqli_affected_rows( $this->dbh ); 1495 } else { 1496 $this->rows_affected = mysql_affected_rows( $this->dbh ); 1497 } 1391 1498 // Take note of the insert_id 1392 1499 if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) { 1393 $this->insert_id = mysql_insert_id($this->dbh); 1500 if ( $this->use_mysqli ) { 1501 $this->insert_id = mysqli_insert_id( $this->dbh ); 1502 } else { 1503 $this->insert_id = mysql_insert_id( $this->dbh ); 1504 } 1394 1505 } 1395 1506 // Return number of rows affected … … 1397 1508 } else { 1398 1509 $num_rows = 0; 1399 while ( $row = @mysql_fetch_object( $this->result ) ) { 1400 $this->last_result[$num_rows] = $row; 1401 $num_rows++; 1510 if ( $this->use_mysqli ) { 1511 while ( $row = @mysqli_fetch_object( $this->result ) ) { 1512 $this->last_result[$num_rows] = $row; 1513 $num_rows++; 1514 } 1515 } else { 1516 while ( $row = @mysql_fetch_object( $this->result ) ) { 1517 $this->last_result[$num_rows] = $row; 1518 $num_rows++; 1519 } 1402 1520 } 1403 1521 … … 1426 1544 } 1427 1545 1428 $this->result = @mysql_query( $query, $this->dbh ); 1546 if ( $this->use_mysqli ) { 1547 $this->result = @mysqli_query( $this->dbh, $query ); 1548 } else { 1549 $this->result = @mysql_query( $query, $this->dbh ); 1550 } 1429 1551 $this->num_queries++; 1430 1552 … … 1764 1886 return; 1765 1887 1766 for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) { 1767 $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i ); 1888 if ( $this->use_mysqli ) { 1889 for ( $i = 0; $i < @mysqli_num_fields( $this->result ); $i++ ) { 1890 $this->col_info[ $i ] = @mysqli_fetch_field( $this->result, $i ); 1891 } 1892 } else { 1893 for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) { 1894 $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i ); 1895 } 1768 1896 } 1769 1897 } … … 1937 2065 */ 1938 2066 function db_version() { 1939 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) ); 2067 if ( $this->use_mysqli ) { 2068 $server_info = mysqli_get_server_info( $this->dbh ); 2069 } else { 2070 $server_info = mysql_get_server_info( $this->dbh ); 2071 } 2072 return preg_replace( '/[^0-9.].*/', '', $server_info ); 1940 2073 } 1941 2074 } -
trunk/tests/phpunit/tests/db.php
r27075 r27250 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
Note: See TracChangeset
for help on using the changeset viewer.