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();
allGroupIds()should beallGroupIds, as this is a property and not a method. That doesn't explain the database error, though. Is yourpublicdirectory next to yourcraftdirectory on the filesystem?