Make WordPress Core

Ticket #12257: 12257.mysqli-fetch2.diff

File 12257.mysqli-fetch2.diff, 7.3 KB (added by sc0ttkclark, 10 years ago)

Fixed issue with mysql_num_rows call which should have been mysqli_num_rows in mysqli mode

  • 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 { 
    126126        var $last_query;
    127127
    128128        /**
    129          * Results of the last query made
    130          *
    131          * @since 0.71
    132          * @access private
    133          * @var array|null
    134          */
    135         var $last_result;
    136 
    137         /**
    138129         * MySQL result, which is either a resource or boolean.
    139130         *
    140131         * @since 0.71
    class wpdb { 
    579570        private $use_mysqli = false;
    580571
    581572        /**
     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        /**
    582583         * Whether we've managed to successfully connect at some point
    583584         *
    584585         * @since 3.9.0
    public function suppress_errors( $suppress = true ) { 
    13601361         * @return void
    13611362         */
    13621363        public function flush() {
    1363                 $this->last_result = array();
    13641364                $this->col_info    = null;
    13651365                $this->last_query  = null;
    1366                 $this->rows_affected = $this->num_rows = 0;
     1366                $this->rows_affected = $this->num_rows = $this->current_row = 0;
    13671367                $this->last_error  = '';
    13681368
    13691369                if ( $this->use_mysqli && $this->result instanceof mysqli_result ) {
    public function query( $query ) { 
    16911691                } else {
    16921692                        $num_rows = 0;
    16931693                        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 );
    16981695                        } 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 );
    17031697                        }
    17041698
    17051699                        // Log number of rows the query returned
    private function _do_query( $query ) { 
    17311725                } else {
    17321726                        $this->result = @mysql_query( $query, $this->dbh );
    17331727                }
     1728               
     1729                $this->current_row = 0;
    17341730                $this->num_queries++;
    17351731
    17361732                if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
    public function get_var( $query = null, $x = 0, $y = 0 ) { 
    20412037                if ( $query ) {
    20422038                        $this->query( $query );
    20432039                }
    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               
    20502043                // 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;
    20522049        }
    20532050
    20542051        /**
    public function get_row( $query = null, $output = OBJECT, $y = 0 ) { 
    20682065                $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
    20692066                if ( $query ) {
    20702067                        $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" );
    20892068                }
     2069               
     2070                return $this->fetch( $output, $y );
    20902071        }
    20912072
    20922073        /**
    public function get_col( $query = null , $x = 0 ) { 
    21082089                }
    21092090
    21102091                $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                        }
    21142099                }
     2100               
    21152101                return $new_array;
    21162102        }
    21172103
    public function get_results( $query = null, $output = OBJECT ) { 
    21332119
    21342120                if ( $query ) {
    21352121                        $this->query( $query );
    2136                 } else {
    2137                         return null;
    21382122                }
    2139 
     2123               
    21402124                $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 ) {
    21482128                                $var_by_ref = get_object_vars( $row );
    21492129                                $key = array_shift( $var_by_ref );
    2150                                 if ( ! isset( $new_array[ $key ] ) )
     2130                               
     2131                                if ( ! isset( $new_array[ $key ] ) ) {
    21512132                                        $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 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                                         }
    21652133                                }
     2134                        } else {
     2135                                $new_array[] = $row;
    21662136                        }
     2137                }
     2138               
     2139                if ( $new_array ) {
    21672140                        return $new_array;
    2168                 } elseif ( strtoupper( $output ) === OBJECT ) {
    2169                         // Back compat for OBJECT being previously case insensitive.
    2170                         return $this->last_result;
    21712141                }
     2142               
    21722143                return null;
    21732144        }
     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        }
    21742202
    21752203        /**
    21762204         * Retrieves the character set for the given table.