Make WordPress Core

Opened 13 months ago

Closed 3 weeks ago

#64130 closed defect (bug) (fixed)

Type error in /wp-includes/class-wpdb.php

Reported by: joehorntw Owned by: mukesh27
Priority: normal Milestone: 7.2
Component: Query Version:
Severity: normal Keywords: has-patch has-unit-tests commit
Cc: Focuses:

Description

Hello,

I find an error message in my wordpress site.

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

#0 /wp-includes/class-wpdb.php(3863): mysqli_num_fields
#1 /wp-includes/class-wpdb.php(3863): wpdb::load_col_info
#2 /wp-includes/class-wpdb.php(786): wpdb::__get
...

I think there should be a type check before callint mysqli_num_fields(),
such as:

<?php
        protected function load_col_info() {
                if ( $this->col_info || !is_object( $this->result ) ) {
                        return;
                }

                $num_fields = mysqli_num_fields( $this->result );

                for ( $i = 0; $i < $num_fields; $i++ ) {
                        $this->col_info[ $i ] = mysqli_fetch_field( $this->result );
                }
        }

Change History (9)

This ticket was mentioned in ​Slack in #core by amykamala. ​View the logs.


13 months ago

This ticket was mentioned in ​PR #13099 on ​WordPress/wordpress-develop by ​@akshat2802.


3 months ago
#2

  • Keywords has-patch has-unit-tests added

Trac ticket: https://core.trac.wordpress.org/ticket/64130
## PR Description
This PR adds a guard to load_col_info() so it returns early when $this->result isn't a mysqli_result, consistent with the existing instanceof mysqli_result check already used in wpdb::flush().

A regression test (test_get_col_info_after_failed_query) is included, which runs an invalid query and then accesses col_info — this reproduces the reported TypeError on unpatched code and passes with the fix applied.
## Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5
Used for: Investigating and confirming the bug against the current trunk, and writing/validating the regression test.

All changes were made and verified by reproducing the failure and confirming the fix in the local test environment.

#3 @mukesh27
6 weeks ago

  • Component Database → Query
  • Milestone Awaiting Review → 7.2
  • Owner set to mukesh27
  • Status new → assigned
  • Version 6.8.3

I was able to reproduce this on trunk (PHP 8.3), and it's not limited to failed queries.

wpdb::load_col_info() passes $this->result straight to mysqli_num_fields(). It is only a mysqli_result after a successful SELECT-type query.

Steps to reproduce

<?php
global $wpdb;
$wpdb->suppress_errors( true );
$wpdb->query( 'SELECT ID FROM table_that_does_not_exist' ); // Or any UPDATE / SET query.
$wpdb->col_info; // Or $wpdb->get_col_info().

Error:

Fatal error: Uncaught TypeError: mysqli_num_fields(): Argument #1 ($result) must be of type mysqli_result, false given in /var/www/src/wp-includes/class-wpdb.php:3950 Stack trace: #0 /var/www/src/wp-includes/class-wpdb.php(3950): mysqli_num_fields(false) #1 /var/www/src/wp-includes/class-wpdb.php(785): wpdb->load_col_info() #2 /var/www/src/wp-content/themes/twentytwentyfour/functions.php(204): wpdb->__get('col_info') #3 /var/www/src/wp-includes/class-wp-hook.php(369): {closure}('') #4 /var/www/src/wp-includes/class-wp-hook.php(393): WP_Hook->apply_filters(NULL, Array) #5 /var/www/src/wp-includes/plugin.php(534): WP_Hook->do_action(Array) #6 /var/www/src/wp-includes/general-template.php(3521): do_action('wp_footer') #7 /var/www/src/wp-includes/template-canvas.php(25): wp_footer() #8 /var/www/src/wp-includes/template-loader.php(132): include('/var/www/src/wp...') #9 /var/www/src/wp-blog-header.php(19): require_once('/var/www/src/wp...') #10 /var/www/src/_index.php(17): require('/var/www/src/wp...') #11 /var/www/src/index.php(19): require_once('/var/www/src/_i...') #12 {main} thrown in /var/www/src/wp-includes/class-wpdb.php on line 3950}}}

#4 @mukesh27
6 weeks ago

  • Keywords changes-requested added

#5 @mukesh27
5 weeks ago

  • Keywords changes-requested removed
  • Status assigned → reviewing

#6 @mukesh27
5 weeks ago

Mark ready for commit

#7 @mukesh27
4 weeks ago

  • Keywords commit added

​@rejaulalomkhan commented on ​PR #13099:


3 weeks ago
#8

Thanks for working on this, @AKSHAT2802! Covering UPDATE and SET queries, not only failed ones, is what makes these tests useful. I tested it locally:

Patch Testing Report

Patch tested: ​https://github.com/WordPress/wordpress-develop/pull/13099

Environment

  • WordPress: 7.2-alpha-63166-src
  • PHP: 8.3.35
  • Server: PHP CLI (PHPUnit 9.6.38)
  • Database: MySQL 8.4.11
  • Browser: N/A (unit tests)
  • OS: macOS (wordpress-develop Docker environment)
  • Theme: N/A
  • MU Plugins: None activated
  • Plugins: None activated

Steps taken

  1. Applied only the new tests to current trunk and ran npm run test:php -- --filter test_get_col_info_without_result_set → all 3 data sets error with TypeError: mysqli_num_fields(): Argument #1 ($result) must be of type mysqli_result (false given for the failed query, true given for UPDATE and SET).
  2. Applied the full PR → the 3 tests pass on single site and multisite.
  3. Ran the wpdb group on single site and multisite, and in random order: 654 tests, no failures (2 skipped, same as trunk).
  4. Changed the guard to only false === $this->result → the update query and set query data sets fail, so the tests also catch a fix that handles failed queries only; reverted.
  5. ✅ Patch is solving the problem: get_col_info() and $wpdb->col_info return null instead of throwing a TypeError when the last query has no result set.

Expected result

  • Reading column info after a query without a result set (failed query, UPDATE, SET) returns null instead of throwing a TypeError.

Additional Notes

  • The new guard matches the existing $this->result instanceof mysqli_result checks in wpdb::flush() and wpdb::query().
  • AI assistance: Claude Code was used to run the tests; results reviewed by me.

#9 @SergeyBiryukov
3 weeks ago

  • Resolution → fixed
  • Status reviewing → closed

In 64458:

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.

Note: See TracTickets for help on using tickets.