#35560 closed defect (bug) (fixed)
Use of undefined constant DB_USER - assumed 'DB_USER'
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 5.0 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Database | Keywords: | has-patch |
Focuses: | Cc: |
Description (last modified by ) ¶
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.
Commits (2)
- [42701] Database: In
require_wp_db()
, check if database constants are defined before using them.… by @SergeyBiryukov 7 years ago - [43722] Database: In
require_wp_db()
, check if database constants are defined before using them.… by @SergeyBiryukov 6 years ago
Pull Requests
- Loading…
Change History (19)
#1
follow-up:
↓ 5
@ Core Committer
9 years ago
- Keywords reporter-feedback added
- Version 4.4.1 deleted
#2
@
9 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
@ Lead Developer
9 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
@
9 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
@ Emeritus Committer
9 years ago
Replying to johnbillion:
This bug is indeed valid, but if you do not have a
wp-config.php
file then WordPress' defaulterror_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
@ Core Committer
9 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
@
9 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.
#10
@ Lead Developer
7 years 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.
#12
@ Core Committer
7 years ago
- Keywords has-patch added; needs-patch removed
35560.diff doesn't work as expected in my testing:
- By the time
setup-config.php
checks the connection,require_wp_db()
is already run viawp-settings.php
. - 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
@ Core Committer
7 years ago
- Owner set to SergeyBiryukov
- Resolution set to fixed
- Status changed from reopened to closed
In 42701:
#14
@ Core Committer
6 years ago
- Resolution fixed deleted
- Status changed from closed to reopened
[42701] should be merged into 5.0 because this error will be promoted to a fatal in PHP 7.3.
#15
@ Core Committer
6 years ago
- Resolution set to fixed
- Status changed from reopened to closed
In 43722:
Thanks for the ticket @mariusvw.
This bug is indeed valid, but if you do not have a
wp-config.php
file then WordPress' defaulterror_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 toerror_reporting( E_ALL )
or by adding awp-config.php
file that did not contain database credentials but did containdefine( '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.