您好,欢迎访问一九零五行业门户网

Load database config file from libraries in CodeIg

sometime you will want to load database config variables from your libraries.in this case, if you use $this-_ci-config-load(database) , you will get an error: $this-_ci = get_instance();$this-_ci-config-load('database'); the error somthing
sometime you will want to load database config variables from your libraries.in this case, if you use $this->_ci->config->load(‘database’), you will get an error:
$this->_ci = &get_instance();$this->_ci->config->load('database');
the error somthing like your application/config/database.php file does not appear to contain a valid configuration array.
the source is in `system/core/config.php#138`:
// codeigniter version 2.1.4if ( ! isset($config) or ! is_array($config)){ if ($fail_gracefully === true) { return false; } show_error('your '.$file_path.' file does not appear to contain a valid configuration array.');}
you can find that the config will lookup an array with the name $config, but in your database’s config file it contains array variable with the name $db?defaultly.this is the reason you got the invalid configuration array error message.
to correctly load database configuration from libraries,you can you code below:
$this->_ci->load->database('default');$db = $this->_ci->db;something::config(['driver'=> 'mysql','host'=> $db->hostname, 'dbname'=> $db->database, 'user'=> $db->username, 'password'=> $db->password]);
notice the `default` is the active group in your database configuration file.
原文地址:load database config file from libraries in codeig, 感谢原作者分享。
其它类似信息

推荐信息