Make WordPress Core

Changeset 64458


Ignore:
Timestamp:
10/17/2026 11:21:34 PM (4 weeks ago)
Author:
SergeyBiryukov
Message:

Database: Avoid a fatal error in wpdb::load_col_info() if the result is not set.

This resolves an error if mysqli_num_fields() is called before a successful SELECT-type query is performed:

mysqli_num_fields(): Argument #1 ($result) must be of type mysqli_result, false given

Developed in ​https://github.com/WordPress/wordpress-develop/pull/13099.

Follow-up to r21472.

Props joehorntw, akshat2802, mukesh27, rejaulalomkhan, SergeyBiryukov.
Fixes #64130.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wpdb.php

    r64436 r64458  
    39523952         */
    39533953        protected function load_col_info() {
    3954                 if ( $this->col_info ) {
     3954                if ( $this->col_info || ! $this->result instanceof mysqli_result ) {
    39553955                        return;
    39563956                }
  • trunk/tests/phpunit/tests/db.php

    r64434 r64458  
    584584                $this->assertSame( array( $wpdb->users ), $wpdb->get_col_info( 'table' ) );
    585585                $this->assertSame( $wpdb->users, $wpdb->get_col_info( 'table', 0 ) );
     586        }
     587
     588        /**
     589         * @ticket 64130
     590         *
     591         * @covers wpdb::get_col_info
     592         *
     593         * @dataProvider data_get_col_info_without_result_set
     594         *
     595         * @param string $query Query that does not produce a result set.
     596         */
     597        public function test_get_col_info_without_result_set( $query ) {
     598                global $wpdb;
     599
     600                $suppress = $wpdb->suppress_errors( true );
     601                // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
     602                $wpdb->query( sprintf( $query, $wpdb->options ) );
     603                $wpdb->suppress_errors( $suppress );
     604
     605                $this->assertNull( $wpdb->get_col_info(), 'get_col_info() should return null.' );
     606                $this->assertNull( $wpdb->col_info, 'The col_info property should be null.' );
     607        }
     608
     609        /**
     610         * Data provider.
     611         *
     612         * @return array<string, string[]>
     613         */
     614        public static function data_get_col_info_without_result_set() {
     615                return array(
     616                        'failed query' => array( 'SELECT ID FROM table_that_does_not_exist' ),
     617                        'update query' => array( "UPDATE %s SET option_value = option_value WHERE option_name = 'blogname'" ),
     618                        'set query'    => array( 'SET @wp_test_64130 = 1' ),
     619                );
    586620        }
    587621
Note: See TracChangeset for help on using the changeset viewer.