Ticket #21663: 21663.5.diff
File 21663.5.diff, 8.3 KB (added by , 12 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 if ( $this->use_mysqli ) { 849 $success = @mysqli_select_db( $dbh, $db ); 850 } else { 851 $success = @mysql_select_db( $db, $dbh ); 852 } 853 if ( ! $success ) { 834 854 $this->ready = false; 835 855 wp_load_translations_early(); 836 856 $this->bail( sprintf( __( '<h1>Can’t select database</h1> … … 866 886 } 867 887 868 888 /** 869 * Real escape, using mysql _real_escape_string()889 * Real escape, using mysqli_real_escape_string() or mysql_real_escape_string() 870 890 * 891 * @see mysqli_real_escape_string() 871 892 * @see mysql_real_escape_string() 872 893 * @since 2.8.0 873 894 * @access private … … 876 897 * @return string escaped 877 898 */ 878 899 function _real_escape( $string ) { 879 if ( $this->dbh ) 880 return mysql_real_escape_string( $string, $this->dbh ); 900 if ( $this->dbh ) { 901 if ( $this->use_mysqli ) { 902 return mysqli_real_escape_string( $this->dbh, $string ); 903 } else { 904 return mysql_real_escape_string( $string, $this->dbh ); 905 } 906 } 881 907 882 908 $class = get_class( $this ); 883 909 _doing_it_wrong( $class, "$class must set a database connection for use with escaping.", E_USER_NOTICE ); … … 1018 1044 function print_error( $str = '' ) { 1019 1045 global $EZSQL_ERROR; 1020 1046 1021 if ( !$str ) 1022 $str = mysql_error( $this->dbh ); 1047 if ( !$str ) { 1048 if ( $this->use_mysqli ) { 1049 $str = mysqli_error( $this->dbh ); 1050 } else { 1051 $str = mysql_error( $this->dbh ); 1052 } 1053 } 1023 1054 $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str ); 1024 1055 1025 1056 if ( $this->suppress_errors ) … … 1122 1153 $this->rows_affected = $this->num_rows = 0; 1123 1154 $this->last_error = ''; 1124 1155 1125 if ( is_resource( $this->result ) ) 1126 mysql_free_result( $this->result ); 1156 if ( is_resource( $this->result ) ) { 1157 if ( $this->use_mysqli ) { 1158 mysqli_free_result( $this->result ); 1159 } else { 1160 mysql_free_result( $this->result ); 1161 } 1162 } 1127 1163 } 1128 1164 1129 1165 /** … … 1139 1175 $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0; 1140 1176 1141 1177 if ( WP_DEBUG ) { 1142 $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1178 if ( $this->use_mysqli ) { 1179 $this->dbh = mysqli_init(); 1180 mysqli_real_connect( $this->dbh, $this->dbhost, $this->dbuser, $this->dbpassword, null, null, null, $client_flags ); 1181 } else { 1182 $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1183 } 1143 1184 } else { 1144 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1185 if ( $this->use_mysqli ) { 1186 $this->dbh = @mysqli_init(); 1187 @mysqli_real_connect( $this->dbh, $this->dbhost, $this->dbuser, $this->dbpassword, null, null, null, $client_flags ); 1188 } else { 1189 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1190 } 1145 1191 } 1146 1192 1147 1193 if ( !$this->dbh ) { … … 1202 1248 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1203 1249 $this->timer_start(); 1204 1250 1205 $this->result = @mysql_query( $query, $this->dbh ); 1251 if ( $this->use_mysqli ) { 1252 $this->result = @mysqli_query( $this->dbh, $query ); 1253 } else { 1254 $this->result = @mysql_query( $query, $this->dbh ); 1255 } 1206 1256 $this->num_queries++; 1207 1257 1208 1258 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1209 1259 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 1210 1260 1211 1261 // If there is an error then take note of it.. 1212 if ( $this->last_error = mysql_error( $this->dbh ) ) { 1213 // Clear insert_id on a subsequent failed insert. 1214 if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) ) 1215 $this->insert_id = 0; 1262 if ( $this->use_mysqli ) { 1263 if ( $this->last_error = mysqli_error( $this->dbh ) ) { 1264 // Clear insert_id on a subsequent failed insert. 1265 if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) ) 1266 $this->insert_id = 0; 1216 1267 1217 $this->print_error(); 1218 return false; 1268 $this->print_error(); 1269 return false; 1270 } 1271 } else { 1272 if ( $this->last_error = mysql_error( $this->dbh ) ) { 1273 // Clear insert_id on a subsequent failed insert. 1274 if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) ) 1275 $this->insert_id = 0; 1276 1277 $this->print_error(); 1278 return false; 1279 } 1219 1280 } 1220 1281 1221 1282 if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) { 1222 1283 $return_val = $this->result; 1223 1284 } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) { 1224 $this->rows_affected = mysql_affected_rows( $this->dbh ); 1285 if ( $this->use_mysqli ) { 1286 $this->rows_affected = mysqli_affected_rows( $this->dbh ); 1287 } else { 1288 $this->rows_affected = mysql_affected_rows( $this->dbh ); 1289 } 1225 1290 // Take note of the insert_id 1226 1291 if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) { 1227 $this->insert_id = mysql_insert_id($this->dbh); 1292 if ( $this->use_mysqli ) { 1293 $this->insert_id = mysqli_insert_id( $this->dbh ); 1294 } else { 1295 $this->insert_id = mysql_insert_id( $this->dbh ); 1296 } 1228 1297 } 1229 1298 // Return number of rows affected 1230 1299 $return_val = $this->rows_affected; 1231 1300 } else { 1232 1301 $num_rows = 0; 1233 while ( $row = @mysql_fetch_object( $this->result ) ) { 1234 $this->last_result[$num_rows] = $row; 1235 $num_rows++; 1302 if ( $this->use_mysqli ) { 1303 while ( $row = @mysqli_fetch_object( $this->result ) ) { 1304 $this->last_result[$num_rows] = $row; 1305 $num_rows++; 1306 } 1307 } else { 1308 while ( $row = @mysql_fetch_object( $this->result ) ) { 1309 $this->last_result[$num_rows] = $row; 1310 $num_rows++; 1311 } 1236 1312 } 1237 1313 1238 1314 // Log number of rows the query returned … … 1574 1650 if ( $this->col_info ) 1575 1651 return; 1576 1652 1577 for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) { 1578 $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i ); 1653 if ( $this->use_mysqli ) { 1654 $num_fields = @mysqli_num_fields( $this->result ); 1655 } else { 1656 $num_fields = @mysql_num_fields( $this->result ); 1579 1657 } 1658 for ( $i = 0; $i < $num_fields; $i++ ) { 1659 if ( $this->use_mysqli ) { 1660 $this->col_info[ $i ] = @mysqli_fetch_field( $this->result, $i ); 1661 } else { 1662 $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i ); 1663 } 1664 } 1580 1665 } 1581 1666 1582 1667 /** … … 1747 1832 * @return false|string false on failure, version number on success 1748 1833 */ 1749 1834 function db_version() { 1750 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) ); 1835 if ( $this->use_mysqli ) { 1836 $server_info = mysqli_get_server_info( $this->dbh ); 1837 } else { 1838 $server_info = mysql_get_server_info( $this->dbh ); 1839 } 1840 return preg_replace( '/[^0-9.].*/', '', $server_info ); 1751 1841 } 1752 1842 }