<p>It looks as if you've sorted thing out (<code>dev.nec...</code> vs <code>nec.dev...</code>)</p>

<p>In general, I find the domain name matching in the config files to be confusing and error prone. I always follow the advice in the <a href="http://craftcookbook.net/recipes/210" rel="nofollow">Foolproof Multi-Environment Config</a> recipe. Put this at the top of your index.php:</p>

<pre><code>switch (strtolower($_SERVER['SERVER_NAME'])) {
  case 'nec.dev':
    define('CRAFT_ENVIRONMENT', 'DEV');
    break;
  case 'nec.dev.stagingdomain.com':
    define('CRAFT_ENVIRONMENT', 'STAGE');
    break;
  case 'nec-inc.com':
    define('CRAFT_ENVIRONMENT', 'LIVE');
    break;
  default:
    exit('No environment defined for '.$_SERVER['SERVER_NAME']);
}
</code></pre>

<p>Then your db.php and general.php will look like this:</p>

<pre><code>return array(
  '*' =&gt; array(...),
  'DEV' =&gt; array(...),
  'STAGE' =&gt; array(...),
  'LIVE' =&gt; array(...),
);
</code></pre>

<p>And when you hit <code>dev.nec.stagingdomain.com</code> you will see that <em>No environment defined</em> error and fairly quickly see what is wrong.</p>

<p>(There is also an <a href="http://craftcookbook.net/recipes/273" rel="nofollow">Enhanced Foolproof Multi-Environment Config</a> recipe)</p>

CC BY-SA 3.0
2016-07-22T15:08:15.570Z
2016-07-22T15:08:15.570Z
Marion Newlevant
<p>I've moved my site to the staging server but now I'm getting a craft can't connect to the database with credentials in craft/config/db.php</p>

<p>Thing is I uploaded this script with the same credentials and it connects just fine. Any idea what I could be doing wrong</p>

<pre><code>&lt;?php
# Fill our vars and run on cli
# $ php -f db-connect-test.php
$dbname = 'name';
$dbuser = 'user';
$dbpass = 'pass';
$dbhost = 'localhost';
$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysql_select_db($dbname) or die("Could not open the db '$dbname'");
​
echo "connected to the database";
</code></pre>

<p>My full db.php file with actual db creds removed - updated to include domain from live site.</p>

<pre><code>&lt;?php
​
/**
 * Database Configuration
 *
 * All of your system's database configuration settings go in here.
 * You can see a list of the default settings in craft/app/etc/config/defaults/db.php
 */
​
return array(
    '*' =&gt; array(
        'tablePrefix' =&gt; 'craft',
    ),
    'nec.dev' =&gt; array(
        'server' =&gt; 'localhost',
        'user' =&gt; 'root',
        'password' =&gt; 'root',
        'database' =&gt; 'nec',
    ),
    'nec.dev.stagingdomain.com' =&gt; array(
        'server' =&gt; 'localhost',
        'user' =&gt; 'staging-user',
        'password' =&gt; 'staging-password',
        'database' =&gt; 'staging-database',
    ),
    'nec-inc.com' =&gt; array(
        'server' =&gt; 'localhost',
        'user' =&gt; '',
        'password' =&gt; '',
        'database' =&gt; '',
    ),
);
</code></pre>

<p>I can't figure out why this doesn't work on staging when the same credentials work with the connection testing script.</p>

CC BY-SA 3.0
2016-07-21T22:22:52.967Z
The domain you're getting the error on is nec.dev.stagingdomain.com?
Brad Bell
CC BY-SA 3.0
2016-07-22T13:42:24.477Z
@BradBell yes. Updating snippet with new, still non working code, based on a response in slack
CreateSean
CC BY-SA 3.0
2016-07-22T13:45:19.113Z
The staging url seems strange. Would `'nec.stagingdomain.com' => array(...)` work?
Damon
CC BY-SA 3.0
2016-07-22T13:47:02.480Z
I'm such an idiot. I realized I had the staging subdomains in the wrong order. it should be dev.nec.stagingdomaain.com now all working
CreateSean
<database><multi-environment>
Staging Server Database Not Connecting
# Staging Server Database Not Connecting

(Asked by: username_0 on 2016-07-21)

I've moved my site to the staging server but now I'm getting a craft can't connect to the database with credentials in craft/config/db.php
Thing is I uploaded this script with the same credentials and it connects just fine. Any idea what I could be doing wrong

My full db.php file with actual db creds removed - updated to include domain from live site.
 array(
        'tablePrefix' => 'craft',
    ),
    'nec.dev' => array(
        'server' => 'localhost',
        'user' => 'root',
        'password' => 'root',
        'database' => 'nec',
    ),
    'nec.dev.stagingdomain.com' => array(
        'server' => 'localhost',
        'user' => 'staging-user',
        'password' => 'staging-password',
        'database' => 'staging-database',
    ),
    'nec-inc.com' => array(
        'server' => 'localhost',
        'user' => '',
        'password' => '',
        'database' => '',
    ),
);

I can't figure out why this doesn't work on staging when the same credentials work with the connection testing script.

**Tags:** database, multi-environment

> **Comments:**
> **username_1**: The domain you're getting the error on is nec.dev.stagingdomain.com?  
> **username_0**: @BradBell yes. Updating snippet with new, still non working code, based on a response in slack  
> **username_2**: The staging url seems strange. Would `'nec.stagingdomain.com' => array(...)` work?  
> **username_0**: I'm such an idiot. I realized I had the staging subdomains in the wrong order. it should be dev.nec.stagingdomaain.com now all working  

---

## 1 answers

### Answer by username_3 on 2016-07-22. Score: 3

It looks as if you've sorted thing out (dev.nec... vs nec.dev...)
In general, I find the domain name matching in the config files to be confusing and error prone. I always follow the advice in the Foolproof Multi-Environment Config recipe. Put this at the top of your index.php:
switch (strtolower($_SERVER['SERVER_NAME'])) {
  case 'nec.dev':
    define('CRAFT_ENVIRONMENT', 'DEV');
    break;
  case 'nec.dev.stagingdomain.com':
    define('CRAFT_ENVIRONMENT', 'STAGE');
    break;
  case 'nec-inc.com':
    define('CRAFT_ENVIRONMENT', 'LIVE');
    break;
  default:
    exit('No environment defined for '.$_SERVER['SERVER_NAME']);
}

Then your db.php and general.php will look like this:
return array(
  '*' => array(...),
  'DEV' => array(...),
  'STAGE' => array(...),
  'LIVE' => array(...),
);

And when you hit dev.nec.stagingdomain.com you will see that No environment defined error and fairly quickly see what is wrong.
(There is also an Enhanced Foolproof Multi-Environment Config recipe)


---

