WPDB: Check that $wpdb->last_result is countable before counting with it.

`wpdb::get_col()` iterates over `$wpdb->last_result`, which can be a non-countable value, should the preceeding query have failed.

Props spacedmonkey, desrosj.
See #45299.


Built from https://develop.svn.wordpress.org/branches/5.0@43934


git-svn-id: http://core.svn.wordpress.org/branches/5.0@43766 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Gary Pendergast 2018-11-22 03:59:44 +00:00
parent e0ff022be6
commit a7a70768ae
2 changed files with 13 additions and 8 deletions

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.0-beta5-43933';
$wp_version = '5.0-beta5-43934';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

View File

@ -2456,8 +2456,10 @@ class wpdb {
$new_array = array();
// Extract the column values
for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {
$new_array[$i] = $this->get_var( null, $x, $i );
if ( $this->last_result ) {
for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {
$new_array[ $i ] = $this->get_var( null, $x, $i );
}
}
return $new_array;
}
@ -2497,11 +2499,14 @@ class wpdb {
} elseif ( $output == OBJECT_K ) {
// Return an array of row objects with keys from column 1
// (Duplicates are discarded)
foreach ( $this->last_result as $row ) {
$var_by_ref = get_object_vars( $row );
$key = array_shift( $var_by_ref );
if ( ! isset( $new_array[ $key ] ) )
$new_array[ $key ] = $row;
if ( $this->last_result ) {
foreach ( $this->last_result as $row ) {
$var_by_ref = get_object_vars( $row );
$key = array_shift( $var_by_ref );
if ( ! isset( $new_array[ $key ] ) ) {
$new_array[ $key ] = $row;
}
}
}
return $new_array;
} elseif ( $output == ARRAY_A || $output == ARRAY_N ) {