I'm getting Error establishing a database connection
when installing WP.
The following prints "Connected" when putting into wp-config.php
:
$mysqli_connection = mysqli_connect(
$_dbsettings['host'],
$_dbsettings['user'],
$_dbsettings['pass'],
trim( $_dbsettings['path'], '/' ),
$_dbsettings['port']
);
if ($mysqli_connection->connect_error) {
echo "Not connected, error: " . $mysqli_connection->connect_error;
}
else {
echo "Connected.";
}
What's going on?
Update:
I use heroku-wp, more about the wp-config.php
here.
I'm getting Error establishing a database connection
when installing WP.
The following prints "Connected" when putting into wp-config.php
:
$mysqli_connection = mysqli_connect(
$_dbsettings['host'],
$_dbsettings['user'],
$_dbsettings['pass'],
trim( $_dbsettings['path'], '/' ),
$_dbsettings['port']
);
if ($mysqli_connection->connect_error) {
echo "Not connected, error: " . $mysqli_connection->connect_error;
}
else {
echo "Connected.";
}
What's going on?
Update:
I use heroku-wp, more about the wp-config.php
here.
2 Answers
Reset to default 1Maybe if you configured the wp-config.php
file like the documentation says (see https://wordpress/support/article/editing-wp-config-php/ )
Sample wp-config.php
file (database section), per the above link:
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */
define( 'DB_USER', 'username_here' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );
Your code is proper for making a 'manual' connection to the database, but it does not set the DEFINES
that WP will use for connections.
Apart from the check that you have to do with your WordPress Database Credentials, also you can check and repair your database
Navigate to your root WordPress file directory and find the wp-config
. This file contains your WordPress installation settings and configuration. You can add this line of code of the bottom :
define( 'WP_ALLOW_REPAIR', true );
This line will enable you from one hand to optimize and on the other hand to repair your database by navigating to www.yourwebsite/wp-admin/maint/repair.php
(you can replace the “yourwebsite” with your actual URL). Then will see a screen with two options repair database and repair and optimize your database. Feel free and choose your option, but the option of repair and optimize will take longer.
In the end, please keep in mind that the database repair page is not secure because anyone can access that URL without having to login. Once you have done with this process and you are done repairing your database, then be sure to remove the line of code you added to wp-config, I refer to define( 'WP_ALLOW_REPAIR', true );