sql >> Base de Datos >  >> RDS >> Mysql

Conexión de base de datos dinámica Codeigniter

Debe proporcionar toda la información de la base de datos en application/config/database.php´

Normalmente, establecería el grupo de base de datos predeterminado, así:

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "database_name";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
$db['default']['swap_pre'] = "";
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

Tenga en cuenta que la información de inicio de sesión y la configuración se proporcionan en la matriz denominada $db['default'] .

Luego puede agregar otra base de datos en una nueva matriz; llamémosla 'otra base de datos'.

$db['anotherdb']['hostname'] = "localhost";
$db['anotherdb']['username'] = "root";
$db['anotherdb']['password'] = "";
$db['anotherdb']['database'] = "another_database_name";
$db['anotherdb']['dbdriver'] = "mysql";
$db['anotherdb']['dbprefix'] = "";
$db['anotherdb']['pconnect'] = TRUE;
$db['anotherdb']['db_debug'] = FALSE;
$db['anotherdb']['cache_on'] = FALSE;
$db['anotherdb']['cachedir'] = "";
$db['anotherdb']['char_set'] = "utf8";
$db['anotherdb']['dbcollat'] = "utf8_general_ci";
$db['anotherdb']['swap_pre'] = "";
$db['anotherdb']['autoinit'] = TRUE;
$db['anotherdb']['stricton'] = FALSE;

Ahora, si desea utilizar la segunda base de datos, simplemente vaya

$DB_another = $this->load->database('anotherdb', TRUE); 

y luego, en lugar de $this->db->foo() , $DB_another->foo()

y puede extender esto a múltiples grupos como este

 $DB2 = $this->load->database('anotherdb1', TRUE); 
 $DB3 = $this->load->database('anotherdb2', TRUE); 

Para obtener más información, consulte aquí:http://ellislab.com/codeigniter/ guía-usuario/base de datos/conexión.html