Ticket #21663: 21663.10.diff
File 21663.10.diff, 8.4 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.9.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 = ( 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 if ( function_exists( 'mysqli_set_charset' ) && $this->has_cap( 'set_charset' ) ) { 653 mysqli_set_charset( $dbh, $charset ); 654 } else { 655 $query = $this->prepare( 'SET NAMES %s', $charset ); 656 if ( ! empty( $collate ) ) 657 $query .= $this->prepare( ' COLLATE %s', $collate ); 658 mysqli_query( $query, $dbh ); 659 } 642 660 } 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 ); 661 if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) { 662 mysql_set_charset( $charset, $dbh ); 663 } else { 664 $query = $this->prepare( 'SET NAMES %s', $charset ); 665 if ( ! empty( $collate ) ) 666 $query .= $this->prepare( ' COLLATE %s', $collate ); 667 mysql_query( $query, $dbh ); 668 } 647 669 } 648 670 } 649 671 } … … 830 852 if ( is_null($dbh) ) 831 853 $dbh = $this->dbh; 832 854 833 if ( !@mysql_select_db( $db, $dbh ) ) { 855 if ( $this->use_mysqli ) { 856 $success = @mysqli_select_db( $dbh, $db ); 857 } else { 858 $success = @mysql_select_db( $db, $dbh ); 859 } 860 if ( ! $success ) { 834 861 $this->ready = false; 835 862 wp_load_translations_early(); 836 863 $this->bail( sprintf( __( '<h1>Can’t select database</h1> … … 866 893 } 867 894 868 895 /** 869 * Real escape, using mysql _real_escape_string()896 * Real escape, using mysqli_real_escape_string() or mysql_real_escape_string() 870 897 * 898 * @see mysqli_real_escape_string() 871 899 * @see mysql_real_escape_string() 872 900 * @since 2.8.0 873 901 * @access private … … 876 904 * @return string escaped 877 905 */ 878 906 function _real_escape( $string ) { 879 if ( $this->dbh ) 880 return mysql_real_escape_string( $string, $this->dbh ); 907 if ( $this->dbh ) { 908 if ( $this->use_mysqli ) { 909 return mysqli_real_escape_string( $this->dbh, $string ); 910 } else { 911 return mysql_real_escape_string( $string, $this->dbh ); 912 } 913 } 881 914 882 915 $class = get_class( $this ); 883 916 _doing_it_wrong( $class, "$class must set a database connection for use with escaping.", E_USER_NOTICE ); … … 1018 1051 function print_error( $str = '' ) { 1019 1052 global $EZSQL_ERROR; 1020 1053 1021 if ( !$str ) 1022 $str = mysql_error( $this->dbh ); 1054 if ( !$str ) { 1055 if ( $this->use_mysqli ) { 1056 $str = mysqli_error( $this->dbh ); 1057 } else { 1058 $str = mysql_error( $this->dbh ); 1059 } 1060 } 1023 1061 $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str ); 1024 1062 1025 1063 if ( $this->suppress_errors ) … … 1122 1160 $this->rows_affected = $this->num_rows = 0; 1123 1161 $this->last_error = ''; 1124 1162 1125 if ( is_resource( $this->result ) ) 1126 mysql_free_result( $this->result ); 1163 if ( is_resource( $this->result ) ) { 1164 if ( $this->use_mysqli ) { 1165 mysqli_free_result( $this->result ); 1166 } else { 1167 mysql_free_result( $this->result ); 1168 } 1169 } 1127 1170 } 1128 1171 1129 1172 /** … … 1138 1181 $new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true; 1139 1182 $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0; 1140 1183 1141 if ( WP_DEBUG ) { 1142 $error_reporting = false; 1143 if ( defined( 'E_DEPRECATED' ) ) { 1144 $error_reporting = error_reporting(); 1145 error_reporting( $error_reporting ^ E_DEPRECATED ); 1184 if ( $this->use_mysqli ) { 1185 $this->dbh = mysqli_init(); 1186 if ( WP_DEBUG ) { 1187 mysqli_real_connect( $this->dbh, $this->dbhost, $this->dbuser, $this->dbpassword, null, null, null, $client_flags ); 1188 } else { 1189 @mysqli_real_connect( $this->dbh, $this->dbhost, $this->dbuser, $this->dbpassword, null, null, null, $client_flags ); 1146 1190 } 1147 $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1148 if ( false !== $error_reporting ) { 1149 error_reporting( $error_reporting ); 1191 } else { 1192 if ( WP_DEBUG ) { 1193 $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1194 } else { 1195 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1150 1196 } 1151 } else {1152 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );1153 1197 } 1154 1198 1155 1199 if ( !$this->dbh ) { … … 1217 1261 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1218 1262 $this->timer_start(); 1219 1263 1220 $this->result = @mysql_query( $query, $this->dbh ); 1264 if ( $this->use_mysqli ) { 1265 $this->result = @mysqli_query( $this->dbh, $query ); 1266 } else { 1267 $this->result = @mysql_query( $query, $this->dbh ); 1268 } 1221 1269 $this->num_queries++; 1222 1270 1223 1271 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1224 1272 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 1225 1273 1226 1274 // If there is an error then take note of it.. 1227 if ( $this->last_error = mysql_error( $this->dbh ) ) { 1275 if ( $this->use_mysqli ) { 1276 $this->last_error = mysqli_error( $this->dbh ); 1277 } else { 1278 $this->last_error = mysql_error( $this->dbh ); 1279 } 1280 1281 if ( $this->last_error ) { 1228 1282 // Clear insert_id on a subsequent failed insert. 1229 1283 if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) ) 1230 1284 $this->insert_id = 0; … … 1236 1290 if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) { 1237 1291 $return_val = $this->result; 1238 1292 } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) { 1239 $this->rows_affected = mysql_affected_rows( $this->dbh ); 1293 if ( $this->use_mysqli ) { 1294 $this->rows_affected = mysqli_affected_rows( $this->dbh ); 1295 } else { 1296 $this->rows_affected = mysql_affected_rows( $this->dbh ); 1297 } 1240 1298 // Take note of the insert_id 1241 1299 if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) { 1242 $this->insert_id = mysql_insert_id($this->dbh); 1300 if ( $this->use_mysqli ) { 1301 $this->insert_id = mysqli_insert_id( $this->dbh ); 1302 } else { 1303 $this->insert_id = mysql_insert_id( $this->dbh ); 1304 } 1243 1305 } 1244 1306 // Return number of rows affected 1245 1307 $return_val = $this->rows_affected; 1246 1308 } else { 1247 1309 $num_rows = 0; 1248 while ( $row = @mysql_fetch_object( $this->result ) ) { 1249 $this->last_result[$num_rows] = $row; 1250 $num_rows++; 1310 if ( $this->use_mysqli ) { 1311 while ( $row = @mysqli_fetch_object( $this->result ) ) { 1312 $this->last_result[$num_rows] = $row; 1313 $num_rows++; 1314 } 1315 } else { 1316 while ( $row = @mysql_fetch_object( $this->result ) ) { 1317 $this->last_result[$num_rows] = $row; 1318 $num_rows++; 1319 } 1251 1320 } 1252 1321 1253 1322 // Log number of rows the query returned … … 1589 1658 if ( $this->col_info ) 1590 1659 return; 1591 1660 1592 for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) { 1593 $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i ); 1661 if ( $this->use_mysqli ) { 1662 for ( $i = 0; $i < @mysqli_num_fields( $this->result ); $i++ ) { 1663 $this->col_info[ $i ] = @mysqli_fetch_field( $this->result, $i ); 1664 } 1665 } else { 1666 for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) { 1667 $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i ); 1668 } 1594 1669 } 1595 1670 } 1596 1671 … … 1762 1837 * @return false|string false on failure, version number on success 1763 1838 */ 1764 1839 function db_version() { 1765 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) ); 1840 if ( $this->use_mysqli ) { 1841 $server_info = mysqli_get_server_info( $this->dbh ); 1842 } else { 1843 $server_info = mysql_get_server_info( $this->dbh ); 1844 } 1845 return preg_replace( '/[^0-9.].*/', '', $server_info ); 1766 1846 } 1767 1847 }