Ticket #21663: 21663.4.diff
File 21663.4.diff, 7.0 KB (added by , 11 years ago) |
---|
-
src/wp-includes/wp-db.php
510 510 public $is_mysql = null; 511 511 512 512 /** 513 * Whether to use mysqli over mysql. 514 * 515 * @since 3.8.0 516 * @access private 517 * @var bool 518 */ 519 private $use_mysqli = false; 520 521 /** 513 522 * Connects to the database server and selects a database 514 523 * 515 524 * PHP5 style constructor for compatibility with PHP5. Does … … 530 539 if ( WP_DEBUG && WP_DEBUG_DISPLAY ) 531 540 $this->show_errors(); 532 541 542 $this->use_mysqli = apply_filters( 'use_mysqli', version_compare( phpversion(), '5.5', '>=' ) && function_exists( 'mysqli_connect' ) ); 543 533 544 $this->init_charset(); 534 545 535 546 $this->dbuser = $dbuser; … … 637 648 if ( ! isset( $collate ) ) 638 649 $collate = $this->collate; 639 650 if ( $this->has_cap( 'collation' ) && ! empty( $charset ) ) { 640 if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' )) {641 mysql_set_charset( $charset, $dbh);651 if ( $this->use_mysqli ) { 652 mysqli_set_charset( $dbh, $charset ); 642 653 } else { 643 $query = $this->prepare( 'SET NAMES %s', $charset ); 644 if ( ! empty( $collate ) ) 645 $query .= $this->prepare( ' COLLATE %s', $collate ); 646 mysql_query( $query, $dbh ); 654 if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) { 655 mysql_set_charset( $charset, $dbh ); 656 } else { 657 $query = $this->prepare( 'SET NAMES %s', $charset ); 658 if ( ! empty( $collate ) ) 659 $query .= $this->prepare( ' COLLATE %s', $collate ); 660 mysql_query( $query, $dbh ); 661 } 647 662 } 648 663 } 649 664 } … … 830 845 if ( is_null($dbh) ) 831 846 $dbh = $this->dbh; 832 847 833 if ( !@mysql_select_db( $db, $dbh ) ) { 848 $success = ( $this->use_mysqli )? @mysqli_select_db( $dbh, $db ) : @mysql_select_db( $db, $dbh ); 849 if ( ! $success ) { 834 850 $this->ready = false; 835 851 wp_load_translations_early(); 836 852 $this->bail( sprintf( __( '<h1>Can’t select database</h1> … … 866 882 } 867 883 868 884 /** 869 * Real escape, using mysql _real_escape_string()885 * Real escape, using mysqli_real_escape_string() or mysql_real_escape_string() 870 886 * 887 * @see mysqli_real_escape_string() 871 888 * @see mysql_real_escape_string() 872 889 * @since 2.8.0 873 890 * @access private … … 877 894 */ 878 895 function _real_escape( $string ) { 879 896 if ( $this->dbh ) 880 return mysql_real_escape_string( $string, $this->dbh );897 return ( $this->use_mysqli )? mysqli_real_escape_string( $this->dbh, $string ) : mysql_real_escape_string( $string, $this->dbh ); 881 898 882 899 $class = get_class( $this ); 883 900 _doing_it_wrong( $class, "$class must set a database connection for use with escaping.", E_USER_NOTICE ); … … 1019 1036 global $EZSQL_ERROR; 1020 1037 1021 1038 if ( !$str ) 1022 $str = mysql_error( $this->dbh );1039 $str = ( $this->use_mysqli )? mysqli_error( $this->dbh ) : mysql_error( $this->dbh ); 1023 1040 $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str ); 1024 1041 1025 1042 if ( $this->suppress_errors ) … … 1122 1139 $this->rows_affected = $this->num_rows = 0; 1123 1140 $this->last_error = ''; 1124 1141 1125 if ( is_resource( $this->result ) ) 1126 mysql_free_result( $this->result ); 1142 if ( is_resource( $this->result ) ) { 1143 if ( $this->use_mysqli ) { 1144 mysqli_free_result( $this->result ); 1145 } else { 1146 mysql_free_result( $this->result ); 1147 } 1148 } 1127 1149 } 1128 1150 1129 1151 /** … … 1139 1161 $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0; 1140 1162 1141 1163 if ( WP_DEBUG ) { 1142 $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1164 if ( $this->use_mysqli ) { 1165 $this->dbh = mysqli_init(); 1166 mysqli_real_connect( $this->dbh, $this->dbhost, $this->dbuser, $this->dbpassword, null, null, null, $client_flags ); 1167 } else { 1168 $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1169 } 1143 1170 } else { 1144 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1171 if ( $this->use_mysqli ) { 1172 $this->dbh = @mysqli_init(); 1173 @mysqli_real_connect( $this->dbh, $this->dbhost, $this->dbuser, $this->dbpassword, null, null, null, $client_flags ); 1174 } else { 1175 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1176 } 1145 1177 } 1146 1178 1147 1179 if ( !$this->dbh ) { … … 1202 1234 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1203 1235 $this->timer_start(); 1204 1236 1205 $this->result = @mysql_query( $query, $this->dbh );1237 $this->result = ( $this->use_mysqli )? @mysqli_query( $this->dbh, $query ) : @mysql_query( $query, $this->dbh ); 1206 1238 $this->num_queries++; 1207 1239 1208 1240 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1209 1241 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 1210 1242 1211 1243 // If there is an error then take note of it.. 1212 if ( $this->last_error = mysql_error( $this->dbh ) ) {1244 if ( $this->last_error = ( $this->use_mysqli )? mysqli_error( $this->dbh ) : mysql_error( $this->dbh ) ) { 1213 1245 // Clear insert_id on a subsequent failed insert. 1214 1246 if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) ) 1215 1247 $this->insert_id = 0; … … 1221 1253 if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) { 1222 1254 $return_val = $this->result; 1223 1255 } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) { 1224 $this->rows_affected = mysql_affected_rows( $this->dbh );1256 $this->rows_affected = ( $this->use_mysqli )? mysqli_affected_rows( $this->dbh ) : mysql_affected_rows( $this->dbh ); 1225 1257 // Take note of the insert_id 1226 1258 if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) { 1227 $this->insert_id = mysql_insert_id($this->dbh);1259 $this->insert_id = ( $this->use_mysqli )? mysqli_insert_id( $this->dbh ) : mysql_insert_id( $this->dbh ); 1228 1260 } 1229 1261 // Return number of rows affected 1230 1262 $return_val = $this->rows_affected; 1231 1263 } else { 1232 1264 $num_rows = 0; 1233 while ( $row = @mysql_fetch_object( $this->result ) ) {1265 while ( $row = ( $this->use_mysqli )? @mysqli_fetch_object( $this->result ) : @mysql_fetch_object( $this->result ) ) { 1234 1266 $this->last_result[$num_rows] = $row; 1235 1267 $num_rows++; 1236 1268 } … … 1574 1606 if ( $this->col_info ) 1575 1607 return; 1576 1608 1577 for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) { 1578 $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i ); 1609 $num_fields = ( $this->use_mysqli )? @mysqli_num_fields( $this->result ) : @mysql_num_fields( $this->result ); 1610 for ( $i = 0; $i < $num_fields; $i++ ) { 1611 $this->col_info[ $i ] = ( $this->use_mysqli )? @mysqli_fetch_field( $this->result, $i ) : @mysql_fetch_field( $this->result, $i ); 1579 1612 } 1580 1613 } 1581 1614 … … 1747 1780 * @return false|string false on failure, version number on success 1748 1781 */ 1749 1782 function db_version() { 1750 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) ); 1783 $server_info = ( $this->use_mysqli )? mysqli_get_server_info( $this->dbh ) : mysql_get_server_info( $this->dbh ); 1784 return preg_replace( '/[^0-9.].*/', '', $server_info ); 1751 1785 } 1752 1786 }