• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
switching databases

#1
Hi all,

I am trying to use / change the database for different organizations / users. In master database, I have db_name for different organizations, I am using  the following code to store the db_name in session.


Code:
$condition = "subdomain =" . "'" . $data['subdomain'] . "' AND " . "status =1";

$this->db->select('id, db_name');
$this->db->from('sublogin');
$this->db->where($condition);
$this->db->limit(1);

$db_name = $this->db->get()->row()->db_name;

// SET db_name for session
$this->session->set_userdata('db_name', $db_name);

in another model, switch the database 


PHP Code:
$db_name $this->session->userdata('db_name');

$this->db->db_select($db_name); 

Its working in localhost but not on my live server.
Whats wrong here?
Reply

#2
Hello
Maybe this will help you

-- in the model.php --
$this->load->database();
$CI = get_instance();
$this->current_db = $CI->load->database('current_db', TRUE);

And then query
$this->current_db->insert("table",$data);


-- now in the database.php --
In the database.php
you could load the current database settings from one global database with the settings per user

$field = "subdomain";
//this could be defined in the index.php or whatever
$subdomain = SUBDOMAIN;

//make connection with your user table, where you store the database names in
$sql = "mysql:host=localhost;dbname=".$user_database;
$pdo_object = new PDO($sql, $rootusername, $rootpassword);
$query = $pdo_object->query('SELECT * FROM config WHERE '.$field.' = "'.$status.'"');
$data = $query->fetchAll(PDO::FETCH_ASSOC);
$pdo_object = null;
$query_builder = TRUE;

$db['current_db'] = array(
'dsn' => 'content',
'hostname' => 'localhost',
'username' => $data[0]["dbuser"],
'password' => $data[0]["dbpassword"],
'database' => $data[0]["dbname"],
'dbdriver' => 'mysqli',
'dbprefix' => '',

etc
Reply

#3
(10-05-2017, 05:33 AM)Thanks for your reply. For switching to another database, the documentation says the following and its working on localhost. why its not working on my live server. https://codeigniter.com/user_guide/datab...cting.html Wrote:
Quote:You don’t need to create separate database configurations if you only need to use a different database on the same connection. You can switch to a different database when you need to, like this:

$this->db->db_select($database2_name);



ponzoHello
Maybe this will help you

-- in the model.php --
$this->load->database();
$CI = get_instance();
$this->current_db = $CI->load->database('current_db', TRUE);

And then query
$this->current_db->insert("table",$data);


-- now in the database.php --
In the database.php
you could load the current database settings from one global database with the settings per user

$field = "subdomain";
//this could be defined in the index.php or whatever
$subdomain = SUBDOMAIN;

//make connection with your user table, where you store the database names in
$sql = "mysql:host=localhost;dbname=".$user_database;
$pdo_object = new PDO($sql, $rootusername, $rootpassword);
$query = $pdo_object->query('SELECT * FROM config WHERE '.$field.' = "'.$status.'"');
$data = $query->fetchAll(PDO::FETCH_ASSOC);
$pdo_object = null;
$query_builder = TRUE;  

$db['current_db'] = array(
'dsn' => 'content',
'hostname' => 'localhost',
'username' => $data[0]["dbuser"],
'password' => $data[0]["dbpassword"],
'database' => $data[0]["dbname"],
'dbdriver' => 'mysqli',
'dbprefix' => '',

etc
Reply

#4
I think the permission problem on other databases for the user, I am logging. Trying to connect to the hosting support.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.