Maison > php教程 > php手册 > Load database config file from libraries in CodeIg

Load database config file from libraries in CodeIg

WBOY
Libérer: 2016-06-06 20:13:45
original
1215 Les gens l'ont consulté

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');
Copier après la connexion

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.');
}
Copier après la connexion

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]);
Copier après la connexion

Notice the `default` is the active group in your database configuration file.

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal