CodeIgniter Forums

Full Version: Running scripts via CLI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello

When i follow the documentation regarding running CLI scripts everything works fine

http://www.codeigniter.com/user_guide/ge...hlight=cli

However when i autoload the database library I get a database error (see attachment)
The database login details are correct because the rest of the app works fine

A partial workaround is to not autoload the database library and to load in in the constructor of each individual controller. But what happens if the controller  function being run on CLI needs to access the database??

Thanks in advance for any help!
Looks like you're using CodeIgniter 3?

It sounds like your environment is messing with you. Do you happen to be setting up your database under an environment, like development, production, etc? If so, it  will assign cli calls to the development environment. If you have a database setup in config/development/database.php then it should attempt to use that config file. If one doesn't exist there, it will try to use the default config/database.php. So, verify that those files would have the correct information in them. 

Alternatively - realize that your controller should probably never need direct access to the database anyway. That should be handled through your models.
(05-05-2015, 11:41 AM)kilishan Wrote: [ -> ]Looks like you're using CodeIgniter 3?

It sounds like your environment is messing with you. Do you happen to be setting up your database under an environment, like development, production, etc? If so, it  will assign cli calls to the development environment. If you have a database setup in config/development/database.php then it should attempt to use that config file. If one doesn't exist there, it will try to use the default config/database.php. So, verify that those files would have the correct information in them. 

Alternatively - realize that your controller should probably never need direct access to the database anyway. That should be handled through your models.

Thanks kilishan for your reply!
Yes, I am using CI 3 and no, i don't have any environments set up. The database login details n config/database.php are correct.

Would you have any example I could follow? I am new to CI but i am intermediate with traditional PHP.

Thanks
If you're using "localhost" for your db connection, try switching to an IP like 127.0.0.1
(05-05-2015, 01:05 PM)CroNiX Wrote: [ -> ]If you're using "localhost" for your db connection, try switching to an IP like 127.0.0.1

Hello There

I have got this to work by editing $db['default'] array in /config/database.php.

I changed hostname key from localhost to 127.0.0.1
and added keys for port and socket

This might help someone else!