diff --git a/wp-includes/wp-db.php b/wp-includes/wp-db.php index e74e4ebce2..3a7b368b8a 100644 --- a/wp-includes/wp-db.php +++ b/wp-includes/wp-db.php @@ -134,10 +134,10 @@ class wpdb { * Saved info on the table column * * @since 1.2.0 - * @access private + * @access protected * @var array */ - var $col_info; + protected $col_info; /** * Saved queries that were executed @@ -515,6 +515,21 @@ class wpdb { return true; } + /** + * PHP5 style magic getter, used to lazy-load expensive data. + * + * @since 3.5.0 + * + * @param string $var The private member to get, and optionally process + * @return mixed The private member + */ + function __get( $var ) { + if ( 'col_info' == $var ) + $this->load_col_info(); + + return $this->$var; + } + /** * Set $this->charset and $this->collate * @@ -902,7 +917,7 @@ class wpdb { $args = $args[0]; $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting - $query = str_replace( '%f' , '%F', $query ); // Force floats to be locale unaware + $query = str_replace( '%f' , '%F', $query ); // Force floats to be locale unaware $query = preg_replace( '|(?last_result = array(); $this->col_info = null; $this->last_query = null; + @mysql_free_result( $this->result ); } /** @@ -1117,19 +1133,12 @@ class wpdb { // Return number of rows affected $return_val = $this->rows_affected; } else { - $i = 0; - while ( $i < @mysql_num_fields( $this->result ) ) { - $this->col_info[$i] = @mysql_fetch_field( $this->result ); - $i++; - } $num_rows = 0; while ( $row = @mysql_fetch_object( $this->result ) ) { $this->last_result[$num_rows] = $row; $num_rows++; } - @mysql_free_result( $this->result ); - // Log number of rows the query returned // and return number of rows selected $this->num_rows = $num_rows; @@ -1457,6 +1466,22 @@ class wpdb { return null; } + /** + * Load the column metadata from the last query. + * + * @since 3.5.0 + * + * @access protected + */ + protected function load_col_info() { + if ( $this->col_info ) + return; + + for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) { + $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i ); + } + } + /** * Retrieve column metadata from the last query. * @@ -1467,6 +1492,8 @@ class wpdb { * @return mixed Column Results */ function get_col_info( $info_type = 'name', $col_offset = -1 ) { + $this->load_col_info(); + if ( $this->col_info ) { if ( $col_offset == -1 ) { $i = 0;