Ticket #12257: 12257.mysqli-fetch-tested.diff
File 12257.mysqli-fetch-tested.diff, 7.2 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..5a95762 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 ) { 2042 2038 $this->query( $query ); 2043 2039 } 2044 2040 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 } 2041 $row = $this->fetch( ARRAY_N, $y ); 2049 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 2068 } 2074 2069 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 } 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[] = $row[$x]; 2096 } else { 2097 $new_array[] = 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( $output ) ) { 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; 2133 } 2134 } else { 2135 $new_array[] = $row; 2152 2136 } 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 arrays 2160 $new_array[] = array_values( get_object_vars( $row ) ); 2161 } else { 2162 // ...column name-keyed row arrays 2163 $new_array[] = get_object_vars( $row ); 2164 } 2137 } 2138 2139 return $new_array; 2140 } 2141 2142 /** 2143 * Fetch a row from the database 2144 * 2145 * @todo Update since 2146 * @since 4.X 2147 * 2148 * @param string $output Optional. one of ARRAY_A | ARRAY_N | OBJECT constants. Return an associative array (column => value, ...), 2149 * a numerically indexed array (0 => value, ...) or an object ( ->column = value ), respectively. 2150 * @param int $y Optional. Row to return. Indexed from 0. 2151 * @return mixed Database query result in format specified by $output or null on failure 2152 */ 2153 public function fetch( $output = OBJECT, $y = null ) { 2154 $this->func_call = "\$db->fetch($output, $y)"; 2155 2156 if ( null === $y ) { 2157 $y = $this->current_row; 2158 2159 if ( $y < $this->num_rows ) { 2160 $this->current_row++; 2161 } 2162 } 2163 else { 2164 $this->current_row = $y; 2165 } 2166 2167 $row = null; 2168 2169 if ( $y < $this->num_rows ) { 2170 if ( $this->use_mysqli ) { 2171 @mysqli_data_seek( $this->result, $y ); 2172 } else { 2173 @mysql_data_seek( $this->result, $y ); 2174 } 2175 2176 if ( $output == OBJECT || strtoupper( $output ) === OBJECT || $output == OBJECT_K ) { 2177 if ( $this->use_mysqli ) { 2178 $row = @mysqli_fetch_object( $this->result ); 2179 } else { 2180 $row = @mysql_fetch_object( $this->result ); 2165 2181 } 2182 } elseif ( $output == ARRAY_A ) { 2183 if ( $this->use_mysqli ) { 2184 $row = @mysqli_fetch_assoc( $this->result ); 2185 } else { 2186 $row = @mysql_fetch_assoc( $this->result ); 2187 } 2188 } elseif ( $output == ARRAY_N ) { 2189 if ( $this->use_mysqli ) { 2190 $row = @mysqli_fetch_row( $this->result ); 2191 } else { 2192 $row = @mysql_fetch_row( $this->result ); 2193 } 2194 } else { 2195 $this->print_error( " \$db->fetch(output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N" ); 2166 2196 } 2167 return $new_array;2168 } elseif ( strtoupper( $output ) === OBJECT ) {2169 // Back compat for OBJECT being previously case insensitive.2170 return $this->last_result;2171 2197 } 2172 return null; 2198 2199 return $row; 2173 2200 } 2174 2201 2175 2202 /**