WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 4 months ago

#35560 closed defect (bug) (fixed)

Use of undefined constant DB_USER - assumed 'DB_USER'

Reported by: mariusvw Owned by: SergeyBiryukov
Milestone: 5.0 Priority: normal
Severity: normal Version:
Component: Database Keywords: has-patch
Focuses: Cc:

Description (last modified by dd32)

At my work we work in strict mode where every error needs to be solved. Even undefined errors will fail the test platform.

In wp-includes/load.php at line 369 it is assumed that DB_USER, DB_PASSWORD, DB_NAME and DB_HOST are defined as constants.

When you don't have a config yet this is not the case. The solution would be muting the error or defining the constants as null.

# Solution 1, prepend above the "new wpdb()":

    if (!defined('DB_USER')) {
        define('DB_USER', null);
    }
    if (!defined('DB_PASSWORD')) {
        define('DB_PASSWORD', null);
    }
    if (!defined('DB_NAME')) {
        define('DB_NAME', null);
    }
    if (!defined('DB_HOST')) {
        define('DB_HOST', null);
    }

# Solution 2, modify the "new wpdb()" line: $wpdb = new wpdb( @DB_USER, @DB_PASSWORD, @DB_NAME, @DB_HOST );

Where I would prefer to use solution 1.

Attachments (2)

35560.diff (572 bytes) - added by jryancard 4 months ago.
35560.2.diff (597 bytes) - added by SergeyBiryukov 4 months ago.

Download all attachments as: .zip

Change History (15)

#1 follow-up: @johnbillion
2 years ago

  • Keywords reporter-feedback added
  • Version 4.4.1 deleted

Thanks for the ticket @mariusvw.

This bug is indeed valid, but if you do not have a wp-config.php file then WordPress' default error_reporting() state means PHP notices are not reported. Under what situation do these notices get reported? I was only able to get PHP to report these errors by editing the above to error_reporting( E_ALL ) or by adding a wp-config.php file that did not contain database credentials but did contain define( 'WP_DEBUG', true ) which is a strange configuration.

The underlying problem should be fixed, but I'm still interested in how these errors are visible on your site.

#2 @mariusvw
2 years ago

They are not visible on a site but they are on our development platform.

It doesn't only occur when there is no wp-config.php. It also occurs when you do have a wp-config.php but still have to run the setup of wordpress.

We have error reporting on -1 and catch all errors with an exception. We do this due we want code without any possible error.

If you want to see an example of the implementation of this, have a look at https://github.com/mariusvw/devtools/blob/master/etc/php/inc/auto_prepend.php

This forces you to fix every error that exists :-)

#3 @dd32
2 years ago

  • Description modified (diff)

It also occurs when you do have a wp-config.php but still have to run the setup of wordpress.

That shouldn't happen, as if you have a wp-config.php file, then those constants should already be defined.

The only scenario where these notices can be generated is when WordPress is yet to have a valid wp-config.php generated for it, or the file lacks those constants because it's using a wp-content/db.php dropin which doesn't use those constants.

#4 @mariusvw
2 years ago

The problem probably only occurs when you don't have a config. Then try to open up the project.

You will be redirected to setup-config.php

Then create the config, you will remain on setup-config.php with the error.

Instead of the error you should actually see the message:

The file 'wp-config.php' already exists. If you need to reset any of the configuration items in this file, please delete it first. You may try installing now.

Where "installing now" would link to install.php like expected.

#5 in reply to: ↑ 1 @rmccue
2 years ago

Replying to johnbillion:

This bug is indeed valid, but if you do not have a wp-config.php file then WordPress' default error_reporting() state means PHP notices are not reported.

If you have a custom error handler set (say, one that throws exceptions), it ignores the error reporting level. I suspect that's what's happening here.

#6 @pento
2 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

It seems this only occurs with an incomplete wp-config.php, which I don't think we can reasonably support.

#7 @mariusvw
2 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

@pento I don't think you should close this ticket.

The issue occurs when you first setup Wordpress, so you don't have a wp-config.php file at all at this point.

#8 @SergeyBiryukov
23 months ago

  • Milestone set to Awaiting Review

#9 @mariusvw
6 months ago

Any progress on this?

#10 @dd32
5 months ago

  • Keywords needs-patch added; reporter-feedback removed
  • Milestone changed from Awaiting Review to 5.0

This does actually need to be fixed, and is an issue. Although most users should not see this warning, as error visibility is turned off at the point in time. wp-admin/setup-config.php includes wp-settings.php which calls require_wp_db() which references these constants.

As of PHP 7.2 the warning reads: (wrapping for readability here only)

Warning: Use of undefined constant DB_USER - assumed 'DB_USER'
(this will throw an Error in a future version of PHP)

According to http://php.net/manual/en/migration72.deprecated.php the next major PHP version will throw a fatal error instead of an undefined constant warning.

I think require_wp_db() can probably just define DB_(USER|PASSWORD|HOST|NAME) etc as null in the event that they're not already defined.

Last edited 5 months ago by dd32 (previous) (diff)

#11 @dd32
5 months ago

#43188 was marked as a duplicate.

@jryancard
4 months ago

#12 @SergeyBiryukov
4 months ago

  • Keywords has-patch added; needs-patch removed

35560.diff doesn't work as expected in my testing:

  1. By the time setup-config.php checks the connection, require_wp_db() is already run via wp-settings.php.
  2. If the values are already defined on first run, this prevents setup-config.php from redefining them to test the connection with user-supplied values.

35560.2.diff is an alternative approach.

#13 @SergeyBiryukov
4 months ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from reopened to closed

In 42701:

Database: In require_wp_db(), check if database constants are defined before using them.

Otherwise, wp-admin/setup-config.php triggers an undefined constant warning in PHP 7.2.

Props mariusvw, jryancard for initial patch.
Fixes #35560.

Note: See TracTickets for help on using tickets.