<p>You don't show how you're defining the <code>'live'</code>, <code>'staging'</code> and <code>'dev'</code> keys for the multi-environment config, but most likely you just need to explicitly set the <a href="https://craftcms.com/docs/php-constants#craft-environment" rel="nofollow"><code>CRAFT_ENVIRONMENT</code></a> PHP constant in your <code>categories.php</code> file to tell Craft which database credentials to choose.</p>

CC BY-SA 3.0
2016-05-16T22:00:28.617Z
2016-05-16T22:00:28.617Z
Brad Bell
<p>I have a PHP file in the <code>public_html</code> titled <code>categories.php</code> with the following code: </p>

<pre><code>namespace Craft;
$craft = require '../craft/app/bootstrap.php';     
$categories = $craft-&gt;categories-&gt;allGroupIds();
</code></pre>

<p>Running this script results in the following message: </p>

<pre><code>Error error
Craft can’t connect to the database with the credentials in craft/config/db.php.
</code></pre>

<p>My <code>craft/config/db.php</code> file looks like this. </p>

<pre><code>return array(
    '*' =&gt; array(
        'tablePrefix' =&gt; 'craft',
    ),
    'live' =&gt; array(
        'server' =&gt; 'localhost',
        'user' =&gt; '',
        'password' =&gt; '',
        'database' =&gt; '',
    ),
    'staging' =&gt; array(
        'server' =&gt; 'localhost',
        'user' =&gt; 'DB_USER',
        'password' =&gt; 'DB_PASSWORD',
        'database' =&gt; 'DB_NAME',
    ),
    'dev' =&gt; array(
        'server' =&gt; 'localhost',
        'user' =&gt; 'root',
        'password' =&gt; 'root',
        'database' =&gt; 'DB_NAME',
    ),
);
</code></pre>

<p>I'm not having database trouble anywhere else, only when I try to call any CategoriesService methods. Is there something that I'm failing to understand here? </p>

<p><strong>Solution:</strong></p>

<p>The CRAFT_ENVIRONMENT constant was being set in public_html/index.php, so my craft/config/db.php file could not choose the correct database credentials. The issue was resolved by setting the constant to 'dev' in my categories.php file, like below.</p>

<pre><code>namespace Craft;
define('CRAFT_ENVIRONMENT', 'dev');
$craft = require '../craft/app/bootstrap.php';
$categories = $craft-&gt;categories-&gt;allGroupIds();
</code></pre>

CC BY-SA 3.0
2016-05-16T19:35:22.157Z
The above code works for me in the Happy Lager example site, except the `allGroupIds()` should be `allGroupIds`, as this is a property and not a method. That doesn't explain the database error, though. Is your `public` directory next to your `craft` directory on the filesystem?
Gavin Anderegg
CC BY-SA 3.0
2016-05-16T22:04:16.293Z
Yes, it sits right next to the craft folder.
jGaiser
<database><php><services>
Getting database error when calling CateogoryService method
# Getting database error when calling CateogoryService method

(Asked by: username_0 on 2016-05-16)

I have a PHP file in the public_html titled categories.php with the following code: 
namespace Craft;
$craft = require '../craft/app/bootstrap.php';     
$categories = $craft->categories->allGroupIds();

Running this script results in the following message: 
Error error
Craft can’t connect to the database with the credentials in craft/config/db.php.

My craft/config/db.php file looks like this. 
return array(
    '*' => array(
        'tablePrefix' => 'craft',
    ),
    'live' => array(
        'server' => 'localhost',
        'user' => '',
        'password' => '',
        'database' => '',
    ),
    'staging' => array(
        'server' => 'localhost',
        'user' => 'DB_USER',
        'password' => 'DB_PASSWORD',
        'database' => 'DB_NAME',
    ),
    'dev' => array(
        'server' => 'localhost',
        'user' => 'root',
        'password' => 'root',
        'database' => 'DB_NAME',
    ),
);

I'm not having database trouble anywhere else, only when I try to call any CategoriesService methods. Is there something that I'm failing to understand here? 
Solution:
The CRAFT_ENVIRONMENT constant was being set in public_html/index.php, so my craft/config/db.php file could not choose the correct database credentials. The issue was resolved by setting the constant to 'dev' in my categories.php file, like below.
namespace Craft;
define('CRAFT_ENVIRONMENT', 'dev');
$craft = require '../craft/app/bootstrap.php';
$categories = $craft->categories->allGroupIds();

**Tags:** database, php, services
> **Comments:**
> **username_1**: The above code works for me in the Happy Lager example site, except the `allGroupIds()` should be `allGroupIds`, as this is a property and not a method. That doesn't explain the database error, though. Is your `public` directory next to your `craft` directory on the filesystem?  
> **username_0**: Yes, it sits right next to the craft folder.  

---

## 1 answers

### Answer by username_2 on 2016-05-16. Score: 0 | Accepted answer

You don't show how you're defining the `'live'`, `'staging'` and `'dev'` keys for the multi-environment config, but most likely you just need to explicitly set the [`CRAFT_ENVIRONMENT`](https://craftcms.com/docs/php-constants#craft-environment) PHP constant in your `categories.php` file to tell Craft which database credentials to choose.


---

