WordPress Database Connection Fix

Solution for "mysqli_get_server_info(): Argument #1 ($mysql) must be of type mysqli, bool given" error

Error Analysis

The error occurs because WordPress is trying to get MySQL server information, but the database connection is failing (returning false instead of a valid MySQL connection object).

Immediate Action Required

This error is preventing WordPress from functioning properly and needs to be fixed immediately.

Step-by-Step Solution

Step 1: Check Database Credentials

Edit your wp-config.php file and verify these settings:

// MySQL settings define('DB_NAME', 'database_name_here'); define('DB_USER', 'username_here'); define('DB_PASSWORD', 'password_here'); define('DB_HOST', 'localhost');

Make sure these values match your actual database credentials.

Step 2: Increase PHP Memory Limits

Add these lines to your wp-config.php file:

// Increase memory limits define('WP_MEMORY_LIMIT', '256M'); define('WP_MAX_MEMORY_LIMIT', '512M'); @ini_set('memory_limit', '512M'); @ini_set('max_execution_time', '300');

Step 3: Add Database Connection Check

Add this code to your theme's functions.php file:

// Prevent database errors during shutdown add_action('shutdown', 'check_db_connection_on_shutdown', 0); function check_db_connection_on_shutdown() { global $wpdb; // Check if database connection is still valid if (!is_resource($wpdb->dbh) && !is_object($wpdb->dbh)) { // Prevent further database operations if connection is lost remove_all_actions('shutdown'); return; } // Test the connection if (!$wpdb->check_connection(false)) { // Connection failed, prevent further database operations remove_all_actions('shutdown'); } } // Alternative: Disable WooCommerce action scheduler during shutdown add_action('shutdown', 'disable_woocommerce_shutdown_actions', 1); function disable_woocommerce_shutdown_actions() { if (class_exists('ActionScheduler_DBStore')) { remove_all_actions('shutdown'); } }

Step 4: Enable Debugging

Add these lines to your wp-config.php to get more information:

// Enable debugging define('WP_DEBUG', true); define('WP_DEBUG_LOG', true); define('WP_DEBUG_DISPLAY', false); @ini_set('display_errors', 0);

This will create a debug.log file in your wp-content directory with detailed error information.

Alternative Solutions

Option A: Repair Database Connection

Create a new file called repair-db.php in your WordPress root directory with this code:

<?php // Repair database connection require_once('wp-load.php'); global $wpdb; // Test connection if ($wpdb->check_connection()) { echo "<h3>Database connection successful!</h3>"; echo "<p>Server info: " . $wpdb->db_server_info() . "</p>"; } else { echo "<h3 style='color: red;'>Database connection failed!</h3>"; echo "<p>Error: " . $wpdb->last_error . "</p>"; } // Close connection $wpdb->close(); echo "<p class='success'>Repair process completed.</p>"; ?>

Run this file by visiting https://yourwebsite.com/repair-db.php in your browser, then delete it afterwards.

Option B: Disable Problematic Plugins

Rename your plugins folder to temporarily disable all plugins:

cd /home1/aurrukwj/public_html/wp-content mv plugins plugins.disabled

Then check if your site works. If it does, rename the folder back and disable plugins one by one to find the culprit:

mv plugins.disabled plugins

Prevention Measures

Database Optimization

Add this to your wp-config.php to optimize database usage:

// Optimize database usage define('WP_POST_REVISIONS', 5); define('EMPTY_TRASH_DAYS', 30); define('AUTOSAVE_INTERVAL', 160); // Seconds

Schedule Regular Database Maintenance

Add this to your theme's functions.php:

// Schedule database optimization function optimize_database_tables() { global $wpdb; $tables = $wpdb->get_results("SHOW TABLES LIKE '" . $wpdb->prefix . "%'", ARRAY_N); foreach ($tables as $table) { $wpdb->query("OPTIMIZE TABLE " . $table[0]); } } add_action('wp_scheduled_cleanup', 'optimize_database_tables');

Final Steps

  1. Implement the solutions above
  2. Check if your website loads properly
  3. Monitor the error logs for any further issues
  4. Remove the debug settings from wp-config.php after fixing the issue

Important Security Note

After implementing these fixes and resolving the issue, make sure to:

If you continue to experience issues, contact your hosting provider for assistance with database server configuration.