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
#3
@
6 weeks ago
- Component Database → Query
- Milestone Awaiting Review → 7.2
- Owner set to
- 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}}}
@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
- 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 withTypeError: mysqli_num_fields(): Argument #1 ($result) must be of type mysqli_result(false givenfor the failed query,true givenforUPDATEandSET). - Applied the full PR → the 3 tests pass on single site and multisite.
- Ran the
wpdbgroup on single site and multisite, and in random order: 654 tests, no failures (2 skipped, same as trunk). - Changed the guard to only
false === $this->result→ theupdate queryandset querydata sets fail, so the tests also catch a fix that handles failed queries only; reverted. - ✅ Patch is solving the problem:
get_col_info()and$wpdb->col_inforeturnnullinstead of throwing aTypeErrorwhen the last query has no result set.
Expected result
- Reading column info after a query without a result set (failed query,
UPDATE,SET) returnsnullinstead of throwing aTypeError.
Additional Notes
- The new guard matches the existing
$this->result instanceof mysqli_resultchecks inwpdb::flush()andwpdb::query(). - AI assistance: Claude Code was used to run the tests; results reviewed by me.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
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->resultisn't amysqli_result, consistent with the existinginstanceof mysqli_resultcheck already used inwpdb::flush().A regression test (
test_get_col_info_after_failed_query) is included, which runs an invalid query and then accessescol_info— this reproduces the reportedTypeErroron 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.