Load database config file from libraries in CodeIg
Jun 06, 2016 pm 08:13 PMSometime 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.4 if ( ! 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, 感谢原作者分享。

Article chaud

Outils chauds Tags

Article chaud

Tags d'article chaud

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Comment résoudre l'erreur de base de données Discuz

Une fuite révèle les spécifications clés des Intel Arrow Lake-U, -H, -HX et -S

Poésie aléatoire d'application native Hongmeng

Utilisez la fonction File.length() de Java pour obtenir la taille du fichier

Comment convertir un blob php en fichier

Comment corriger l'erreur 0xC00CE556 renvoyée par l'analyseur .NET

Renommez les fichiers à l'aide de la fonction File.renameTo() de Java

Utilisez la fonction File.getParentFile() de Java pour obtenir le répertoire parent du fichier
