Welcome Guest, Not a member yet? Register   Sign In
Conencting to multiple databases at same time, without knowing second database details to start with
#1

[eluser]DavidHopkins[/eluser]
Hello all, im new to codeigniter and am really enjoying it.

However i have a slight issue with database connections, well two databases connections to be honest.

My application i am trying to build has a default database that has all the users details etc. THis is used to log in and carry out other basic funcitons. I have set this database up using the config inside database.php and can use it by just calling $this->db->

My problem comes however when i try to add a second database, which is unique to each person who logs in so i dont know their details until they have logged in.

I wish to set this database up in such a way i can access it like $this->db2->

Can anyone please help me with this ?

SOme example code would be great !

THanks

David Hopkins
#2

[eluser]Kamarg[/eluser]
Code:
// Get users personal database info from whatever location it is stored in
$user_db_info = get_user_database_info($user_id);

// Error checking that we have data for the user's database
if(!$user_db_info)
  show_error("ZOMG! You don't exist!");

// Create a config array for loading the database
$config['hostname'] = $user_db_info->hostname;
$config['username'] = $user_db_info->username;
$config['password'] = $user_db_info->password;
$config['database'] = $user_db_info->database;
$config['dbdriver'] = $user_db_info->driver;
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";

// Load the database as $this->db2
$this->db2 = $this->load->database($config, TRUE);

// Do some cool personal stuff for each user
super_duper_awesome_personalization_code_goes_here();

Of course, I can't help but think that any application design that gives every single user two connections to the database server is probably not going to scale all that well. If possible you may want to rethink your architecture.

Edit: Guess escaping quotes doesn't fix the color scheme.
#3

[eluser]DavidHopkins[/eluser]
Hey, thanks for the reply, in relation to your statement about not scaling all that well, could you please explain a little bit ? im unsure how it will effect it?

Thanks
#4

[eluser]cideveloper[/eluser]
Code:
$query = $this->db->query("select user_db, user_username, user_password from master_table where user_id='user_id'");

if ($query->num_rows() > 0)
{
$row = $query->row();

$dsn = 'mysql://'.$row->user_username.':'.$row->user_password.'@localhost/'.$row->user_db;
$user_db = $this->load->database($dsn, TRUE);
}

$user_db->query("select * from user_db_table");

All user stuff will now use the last line. Obviously this is a very simplified version as you probably should be using models for database actions




Theme © iAndrew 2016 - Forum software by © MyBB