<p>Move the check the SERVER_NAME <code>switch</code> up above the <code>require_once $path;</code>. You need to set <code>CRAFT_ENVIRONMENT</code> earlier.</p>

CC BY-SA 3.0
2014-06-16T17:23:41.900Z
That did it. Why would the staging and dev sites work though and not the live site?
Stuart McCoy
CC BY-SA 3.0
2014-06-16T17:26:55.133Z
If you had `staging` as part of the staging url, it would match (same with dev). I'm guessing there is no `live` in the live site url.
Marion Newlevant
CC BY-SA 3.0
2014-06-16T17:33:59.090Z
Not sure why I placed that code at the bottom of the index page on this site because two other sites I'm working on currently have it where it should go, above the //do not edit below this line comment, but there you have it. Problem solved. Craft SE to the rescue.
Stuart McCoy
CC BY-SA 3.0
2014-06-16T17:19:34.310Z
2014-06-16T17:19:34.310Z
Marion Newlevant
<p>I'm trying to push a site live today and am running into a problem I'm having trouble solving. I have a staging site which is a subdomain on the live server and everything works fine there but when I try to load the live site I get the following error (strange characters and all):</p>

<blockquote>
  <p>An exception has been thrown during the rendering of a template ("Craft can't connect to the database with the credentials in craft/config/db.php.") in "_includes/_main_nav" at line 19.</p>
</blockquote>

<p>The only difference between the two config files is the database name in db.php and the siteUrl and fileSystemPath in general.php. I do have a multi-environment config based on the one at straughtupcraft.com but I don't see any other difference between my live and staging environments other then what is already noted above.</p>

<p>Anyone run across this problem yet? Solutions?</p>

<p><strong>UPDATE:</strong></p>

<p><strong>index.php:</strong></p>

<pre><code>&lt;?php

//die('hey');


// Path to your craft/ folder
$craftPath = '../craft';

// Do not edit below this line
$path = rtrim($craftPath, '/').'/app/index.php';

if (!is_file($path))
{
    exit('Could not find your craft/ folder. Please ensure that &lt;strong&gt;&lt;code&gt;$craftPath&lt;/code&gt;&lt;/strong&gt; is set correctly in '.__FILE__);
}

require_once $path;


// Check the SERVER_NAME variable ourselves
switch ($_SERVER['SERVER_NAME']) 
{
    // If the SERVER_NAME variable matches our case, 
    // assign the CRAFT_ENVIRONMENT variable a keyword 
    // that identifies this environment that we can 
    // use in our multi-environment config

    case 'site.com' :
        define('CRAFT_ENVIRONMENT', 'live');
        break;

    case 'staging.site.com' :
        define('CRAFT_ENVIRONMENT', 'staging');
        break;

    default :
        define('CRAFT_ENVIRONMENT', 'dev');
        break;
}
</code></pre>

<p><strong>general.php</strong>
    

<pre><code>return array(
    '*' =&gt; array(
        'cpTrigger' =&gt; 'site_admin',
        'omitScriptNameInUrls' =&gt; true,
        'slugWordSeparator' =&gt; "_",
    ),
    'live' =&gt; array(
        'environmentVariables' =&gt; array(
            'siteUrl' =&gt; 'http://site.com',
            'fileSystemPath' =&gt; '/path/to/domains/site.com/html'
        )
    ),
    'staging' =&gt; array(
        'devMode' =&gt; true,
        'environmentVariables' =&gt; array(
            'siteUrl' =&gt; 'http://staging.site.com',
            'fileSystemPath' =&gt; '/path/to/domains/staging.site.com/html'
        )
    ),
    'dev' =&gt; array(
        'devMode' =&gt; true,
        'environmentVariables' =&gt; array(
            'siteUrl' =&gt; 'http://site.dev',
            'fileSystemPath' =&gt; '/users/stubear/sites/site/dev/html'
        )
    ),
);
</code></pre>

<p><strong>db.php</strong>
    

<pre><code>return array(
    '*' =&gt; array(
        'tablePrefix' =&gt; 'craft',
    ),
    'live' =&gt; array(
        'server' =&gt; 'server',
        'user' =&gt; 'username',
        'password' =&gt; '********',
        'database' =&gt; 'craft_live_db',
    ),
    'staging' =&gt; array(
        'server' =&gt; 'server',
        'user' =&gt; 'username',
        'password' =&gt; '********',
        'database' =&gt; 'craft_staging_db',
    ),
    'dev' =&gt; array(
        'server' =&gt; 'localhost',
        'user' =&gt; 'root',
        'password' =&gt; 'root',
        'database' =&gt; 'site-dev',
    ),
);
</code></pre>

CC BY-SA 3.0
2014-06-16T16:05:39.310Z
Can you post your db and config file (with masked passwords)?
Victor
CC BY-SA 3.0
2014-06-16T16:25:42.493Z
Updated question to add requested files. I've removed all the secure information from them and scrubbed the domain name as well.
Stuart McCoy
CC BY-SA 3.0
2014-06-16T16:29:42.507Z
I can't see anything wrong with the config. Can you check the db-connection and also add an echo to the switch, to be sure the server-name gets matched correctly?
Victor
CC BY-SA 3.0
2014-06-16T16:41:08.700Z
Can you enable devMode, then reproduce the error then check your craft/storage/runtime/logs files for an underlying error?
Brad Bell
CC BY-SA 3.0
2014-06-16T16:53:52.110Z
Brad, it appears to be throwing a 404 error per this line in the log file (path edited):

2014/06/16 16:48:28 [error] [exception.Craft\HttpException.404] exception 'Craft\HttpException' in /path/to/domains/site.com/craft/app/etc/web/WebApp.php:751
Stuart McCoy
<config><database>
Cannot connect to database with credentials provided in db.php
# Cannot connect to database with credentials provided in db.php

(Asked by: username_0 on 2014-06-16)

I'm trying to push a site live today and am running into a problem I'm having trouble solving. I have a staging site which is a subdomain on the live server and everything works fine there but when I try to load the live site I get the following error (strange characters and all):

An exception has been thrown during the rendering of a template ("Craft can't connect to the database with the credentials in craft/config/db.php.") in "_includes/_main_nav" at line 19.

The only difference between the two config files is the database name in db.php and the siteUrl and fileSystemPath in general.php. I do have a multi-environment config based on the one at straughtupcraft.com but I don't see any other difference between my live and staging environments other then what is already noted above.
Anyone run across this problem yet? Solutions?
UPDATE:
index.php:
$craftPath is set correctly in '.__FILE__);
}

require_once $path;


// Check the SERVER_NAME variable ourselves
switch ($_SERVER['SERVER_NAME']) 
{
    // If the SERVER_NAME variable matches our case, 
    // assign the CRAFT_ENVIRONMENT variable a keyword 
    // that identifies this environment that we can 
    // use in our multi-environment config

    case 'site.com' :
        define('CRAFT_ENVIRONMENT', 'live');
        break;

    case 'staging.site.com' :
        define('CRAFT_ENVIRONMENT', 'staging');
        break;

    default :
        define('CRAFT_ENVIRONMENT', 'dev');
        break;
}

general.php
return array(
    '*' => array(
        'cpTrigger' => 'site_admin',
        'omitScriptNameInUrls' => true,
        'slugWordSeparator' => "_",
    ),
    'live' => array(
        'environmentVariables' => array(
            'siteUrl' => 'http://site.com',
            'fileSystemPath' => '/path/to/domains/site.com/html'
        )
    ),
    'staging' => array(
        'devMode' => true,
        'environmentVariables' => array(
            'siteUrl' => 'http://staging.site.com',
            'fileSystemPath' => '/path/to/domains/staging.site.com/html'
        )
    ),
    'dev' => array(
        'devMode' => true,
        'environmentVariables' => array(
            'siteUrl' => 'http://site.dev',
            'fileSystemPath' => '/users/stubear/sites/site/dev/html'
        )
    ),
);

db.php
return array(
    '*' => array(
        'tablePrefix' => 'craft',
    ),
    'live' => array(
        'server' => 'server',
        'user' => 'username',
        'password' => '********',
        'database' => 'craft_live_db',
    ),
    'staging' => array(
        'server' => 'server',
        'user' => 'username',
        'password' => '********',
        'database' => 'craft_staging_db',
    ),
    'dev' => array(
        'server' => 'localhost',
        'user' => 'root',
        'password' => 'root',
        'database' => 'site-dev',
    ),
);

**Tags:** config, database
> **Comments:**
> **username_1**: Can you post your db and config file (with masked passwords)?  
> **username_0**: Updated question to add requested files. I've removed all the secure information from them and scrubbed the domain name as well.  
> **username_1**: I can't see anything wrong with the config. Can you check the db-connection and also add an echo to the switch, to be sure the server-name gets matched correctly?  
> **username_2**: Can you enable devMode, then reproduce the error then check your craft/storage/runtime/logs files for an underlying error?  
> **username_0**: Brad, it appears to be throwing a 404 error per this line in the log file (path edited):  2014/06/16 16:48:28 [error] [exception.Craft\HttpException.404] exception 'Craft\HttpException' in /path/to/domains/site.com/craft/app/etc/web/WebApp.php:751  

---

## 1 answers

### Answer by username_3 on 2014-06-16. Score: 10 | Accepted answer

Move the check the SERVER\_NAME `switch` up above the `require_once $path;`. You need to set `CRAFT_ENVIRONMENT` earlier.

> **Comments:**
> **username_0**: That did it. Why would the staging and dev sites work though and not the live site?  
> **username_3**: If you had `staging` as part of the staging url, it would match (same with dev). I'm guessing there is no `live` in the live site url.  
> **username_0**: Not sure why I placed that code at the bottom of the index page on this site because two other sites I'm working on currently have it where it should go, above the //do not edit below this line comment, but there you have it. Problem solved. Craft SE to the rescue.  

---

