Ticket #12257: 12257.mysqli-fetch2.diff
File 12257.mysqli-fetch2.diff, 7.3 KB (added by , 10 years ago) |
---|
-
wp-includes/wp-db.php
diff --git a/wp-includes/wp-db.php b/wp-includes/wp-db.php index 5393240..00d282a 100644
a b class wpdb { 126 126 var $last_query; 127 127 128 128 /** 129 * Results of the last query made130 *131 * @since 0.71132 * @access private133 * @var array|null134 */135 var $last_result;136 137 /**138 129 * MySQL result, which is either a resource or boolean. 139 130 * 140 131 * @since 0.71 … … class wpdb { 579 570 private $use_mysqli = false; 580 571 581 572 /** 573 * Current row pointer 574 * 575 * @todo Update since 576 * @since 4.X 577 * @access private 578 * @var int 579 */ 580 var $current_row = 0; 581 582 /** 582 583 * Whether we've managed to successfully connect at some point 583 584 * 584 585 * @since 3.9.0 … … public function suppress_errors( $suppress = true ) { 1360 1361 * @return void 1361 1362 */ 1362 1363 public function flush() { 1363 $this->last_result = array();1364 1364 $this->col_info = null; 1365 1365 $this->last_query = null; 1366 $this->rows_affected = $this->num_rows = 0;1366 $this->rows_affected = $this->num_rows = $this->current_row = 0; 1367 1367 $this->last_error = ''; 1368 1368 1369 1369 if ( $this->use_mysqli && $this->result instanceof mysqli_result ) { … … public function query( $query ) { 1691 1691 } else { 1692 1692 $num_rows = 0; 1693 1693 if ( $this->use_mysqli && $this->result instanceof mysqli_result ) { 1694 while ( $row = @mysqli_fetch_object( $this->result ) ) { 1695 $this->last_result[$num_rows] = $row; 1696 $num_rows++; 1697 } 1694 $num_rows = @mysqli_num_rows( $this->result ); 1698 1695 } elseif ( is_resource( $this->result ) ) { 1699 while ( $row = @mysql_fetch_object( $this->result ) ) { 1700 $this->last_result[$num_rows] = $row; 1701 $num_rows++; 1702 } 1696 $num_rows = @mysql_num_rows( $this->result ); 1703 1697 } 1704 1698 1705 1699 // Log number of rows the query returned … … private function _do_query( $query ) { 1731 1725 } else { 1732 1726 $this->result = @mysql_query( $query, $this->dbh ); 1733 1727 } 1728 1729 $this->current_row = 0; 1734 1730 $this->num_queries++; 1735 1731 1736 1732 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) { … … public function get_var( $query = null, $x = 0, $y = 0 ) { 2041 2037 if ( $query ) { 2042 2038 $this->query( $query ); 2043 2039 } 2044 2045 // Extract var out of cached results based x,y vals 2046 if ( !empty( $this->last_result[$y] ) ) { 2047 $values = array_values( get_object_vars( $this->last_result[$y] ) ); 2048 } 2049 2040 2041 $row = $this->fetch( ARRAY_N ); 2042 2050 2043 // If there is a value return it else return null 2051 return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null; 2044 if ( $row && isset( $row[$x] ) && $row[$x] !== '' ) { 2045 return $row[$x]; 2046 } 2047 2048 return null; 2052 2049 } 2053 2050 2054 2051 /** … … public function get_row( $query = null, $output = OBJECT, $y = 0 ) { 2068 2065 $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; 2069 2066 if ( $query ) { 2070 2067 $this->query( $query ); 2071 } else {2072 return null;2073 }2074 2075 if ( !isset( $this->last_result[$y] ) )2076 return null;2077 2078 if ( $output == OBJECT ) {2079 return $this->last_result[$y] ? $this->last_result[$y] : null;2080 } elseif ( $output == ARRAY_A ) {2081 return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null;2082 } elseif ( $output == ARRAY_N ) {2083 return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null;2084 } elseif ( strtoupper( $output ) === OBJECT ) {2085 // Back compat for OBJECT being previously case insensitive.2086 return $this->last_result[$y] ? $this->last_result[$y] : null;2087 } else {2088 $this->print_error( " \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N" );2089 2068 } 2069 2070 return $this->fetch( $output, $y ); 2090 2071 } 2091 2072 2092 2073 /** … … public function get_col( $query = null , $x = 0 ) { 2108 2089 } 2109 2090 2110 2091 $new_array = array(); 2111 // Extract the column values 2112 for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) { 2113 $new_array[$i] = $this->get_var( null, $x, $i ); 2092 2093 while ( $row = $this->fetch( ARRAY_N ) ) { 2094 if ( isset( $row[$x] ) && $row[$x] !== '' ) { 2095 $new_array[$i] = $row[$x]; 2096 } else { 2097 $new_array[$i] = null; 2098 } 2114 2099 } 2100 2115 2101 return $new_array; 2116 2102 } 2117 2103 … … public function get_results( $query = null, $output = OBJECT ) { 2133 2119 2134 2120 if ( $query ) { 2135 2121 $this->query( $query ); 2136 } else {2137 return null;2138 2122 } 2139 2123 2140 2124 $new_array = array(); 2141 if ( $output == OBJECT ) { 2142 // Return an integer-keyed array of row objects 2143 return $this->last_result; 2144 } elseif ( $output == OBJECT_K ) { 2145 // Return an array of row objects with keys from column 1 2146 // (Duplicates are discarded) 2147 foreach ( $this->last_result as $row ) { 2125 2126 while ( $row = $this->fetch( $object ) ) { 2127 if ( $output == OBJECT_K ) { 2148 2128 $var_by_ref = get_object_vars( $row ); 2149 2129 $key = array_shift( $var_by_ref ); 2150 if ( ! isset( $new_array[ $key ] ) ) 2130 2131 if ( ! isset( $new_array[ $key ] ) ) { 2151 2132 $new_array[ $key ] = $row; 2152 }2153 return $new_array;2154 } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {2155 // Return an integer-keyed array of...2156 if ( $this->last_result ) {2157 foreach( (array) $this->last_result as $row ) {2158 if ( $output == ARRAY_N ) {2159 // ...integer-keyed row arrays2160 $new_array[] = array_values( get_object_vars( $row ) );2161 } else {2162 // ...column name-keyed row arrays2163 $new_array[] = get_object_vars( $row );2164 }2165 2133 } 2134 } else { 2135 $new_array[] = $row; 2166 2136 } 2137 } 2138 2139 if ( $new_array ) { 2167 2140 return $new_array; 2168 } elseif ( strtoupper( $output ) === OBJECT ) {2169 // Back compat for OBJECT being previously case insensitive.2170 return $this->last_result;2171 2141 } 2142 2172 2143 return null; 2173 2144 } 2145 2146 /** 2147 * Fetch a row from the database 2148 * 2149 * @todo Update since 2150 * @since 4.X 2151 * 2152 * @param string $output Optional. one of ARRAY_A | ARRAY_N | OBJECT constants. Return an associative array (column => value, ...), 2153 * a numerically indexed array (0 => value, ...) or an object ( ->column = value ), respectively. 2154 * @param int $y Optional. Row to return. Indexed from 0. 2155 * @return mixed Database query result in format specified by $output or null on failure 2156 */ 2157 public function fetch( $output = OBJECT, $y = null ) { 2158 $this->func_call = "\$db->fetch($output, $y)"; 2159 2160 if ( null === $y ) { 2161 $y = $this->current_row; 2162 2163 if ( $y < $this->num_rows ) { 2164 $this->current_row++; 2165 } 2166 } 2167 2168 $row = null; 2169 2170 if ( $y < $this->num_rows ) { 2171 if ( $this->use_mysqli ) { 2172 @mysqli_data_seek( $this->result, $y ); 2173 } else { 2174 @mysql_data_seek( $this->result, $y ); 2175 } 2176 2177 if ( $output == OBJECT || strtoupper( $output ) === OBJECT || $output == OBJECT_K ) { 2178 if ( $this->use_mysqli ) { 2179 $row = @mysqli_fetch_object( $this->result ); 2180 } else { 2181 $row = @mysql_fetch_object( $this->result ); 2182 } 2183 } elseif ( $output == ARRAY_A ) { 2184 if ( $this->use_mysqli ) { 2185 $row = @mysqli_fetch_assoc( $this->result ); 2186 } else { 2187 $row = @mysql_fetch_assoc( $this->result ); 2188 } 2189 } elseif ( $output == ARRAY_N ) { 2190 if ( $this->use_mysqli ) { 2191 $row = @mysqli_fetch_row( $this->result ); 2192 } else { 2193 $row = @mysql_fetch_row( $this->result ); 2194 } 2195 } else { 2196 $this->print_error( " \$db->fetch(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N" ); 2197 } 2198 } 2199 2200 return $row; 2201 } 2174 2202 2175 2203 /** 2176 2204 * Retrieves the character set for the given table.