Using Codeigniter configuration file sessions

Asked

Viewed 477 times

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, only I need this information from the database in the file database php..

My question is the following, how do I recover this information that are in sessions, in this configuration file?

1 answer

1


For your case, I believe you have to create an extra setting while running the program. From agreement with 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 in the array $config the parameters you have in the session (outside the file database.php).

$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";
  • I tried this way, but I was not successful, because he always looked for the settings of the file 'database.php'

  • This you would change not in the configuration file, but in the execution of your controller for example.

  • Still unsuccessful, when I do so it returns me an error "Call to a Member Function select() on a non-object in"

  • @Mathdesigner47 could insert some snippets of code from your application ? I tested here on a new project and it worked.

  • 1

    Buddy, there was a typo of mine, now it’s working perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.