"Connection timed out" error occurs when I try to connect to the database.

I keep getting the error: “Connection timed out” whenever I try to connect to the database on the server, and I was wondering if anyone has advice on how to fix it.

My connection code:

        define("DB_NAME", "a database name");
        define("DB_SERVER", "localhost");
        define("DB_USERNAME", "username");
        define("DB_PASSWORD", "password");
        
        include_once("database.php");

In the linked file:

        $db = new mysqli(DB_NAME, DB_SERVER, DB_USERNAME, DB_PASSWORD);
        
        if ($db->connect_error) {
        
            // show an error page
        
        	exit();
        }

This seems to work on my development server, but not here.

Any help would be appreciated.

The DB_SERVER param should not be “localhost”. The correct MySQL hostname can be found in your control panel.

@Admin said:
The DB_SERVER param should not be “localhost”. The correct MySQL hostname can be found in your control panel.

I changed DB_SERVER to what the control panel says is the “MySQL Host Name”, but the issue persists.

Please check the correct order of the parameters. The mysqli constructor takes the hostname first, then username, password and database name. You provide the database name as first argument, which of course is not a valid database hostname. Moving the DB_NAME to the end should fix the problem.

@Admin said:
Please check the correct order of the parameters. The mysqli constructor takes the hostname first, then username, password and database name. You provide the database name as first argument, which of course is not a valid database hostname. Moving the DB_NAME to the end should fix the problem.

Seems to be working now. Thank you!!