Using sessions in configuration file in CodeIgniter

2

In my project, I need to do some tasks, which depend on information that is in the database. I store this information in sessions, to use throughout my project.

One of these tasks is to connect to several databases, and the information from these databases comes from a main database, so I store this information in sessions, but I need this information from the database in the file database.php .

My question is, how do I retrieve this information from sessions in this configuration file?

    
asked by anonymous 04.03.2015 / 13:13

1 answer

1

In your case, I believe you have to create an extra configuration while running the program. From according to the documentation you can change the DB configuration as follows:

<?php

$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";

$this->load->database($config);

Or use two settings at the same time:

$dbBase = $this->load->database('default', TRUE);
$dbUser = $this->load->database($config, TRUE);

// Dispara query para o banco principal
$dbBase->query();

// Dispara query para o banco do usuario
$dbUser->query();

Just change the array $config of the parameters you have in the session (outside the database.php file).

$config['hostname'] = $this->session->userdata('host');
$config['username'] = $this->session->userdata('user');
$config['password'] = $this->session->userdata('password');
$config['database'] = $this->session->userdata('database');
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
    
04.03.2015 / 13:55