Getting database error when calling CateogoryService method 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-&gt;categories-&gt;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(
    '*' =&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',
    ),
);


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-&gt;categories-&gt;allGroupIds();
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 PHP constant in your categories.php file to tell Craft which database credentials to choose.