Reputation:
I am working with application ( school management system ) i am to create a multi database driven app one separate database for each year.
my approach is that >> i made a form with select option something like that
<form method="POST" action="host/configure/select_database">
<select name="database">
<option value="2010">Year 2010</option>
<option value="2011">Year 2011</option>
<option value="2012">Year 2012</option>
<option value="2013">Year 2013</option>
<option value="2014">Year 2014</option>
<option value="2015">Year 2015</option>
<option value="soon...">So On ... </option>
</select>
<input type="submit" value="choose year"/>
</form>
when user select any year from given option my select_database
will set database in order to work with
configure.php (controller)
public function select_database(){
session_start();
$_SESSION['current_database'] = $this->input->post('database');
redirect_to('/');
}
here is my selection of database code in database.php
database.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$database = isset($_SESSION['current_database'])?$_SESSION['current_database']:'2015';
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'Username';
$db['default']['password'] = "DbPassword";
$db['default']['database'] = $database;
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$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;
this is my approach is there any other best way to change database from user choice please make me sure am i correct or not thanks
Upvotes: 4
Views: 1763
Reputation: 1672
If you happen to be on the same domain and no need to switch connection credentials and will just be changing db momentarily do the following:
//get the previous database in use
$desired_db = '2005';
$current_db = $this->db->database;
$this->db->database = $desired_db;
$this->load->database();
$this->db->db_select(); // this line is important
// run your queries,
$this->db->select();
$script = "db queries";
$query = $this->db->query($script);
$result = $query->result();
// switch back when you are done
$this->db->database = $current_db;
$this->load->database();
$this->db->db_select(); // this line is important
You can also send different connection credentials like this.
$this->db->hostname = $hostname;
$this->db->username = $username;
$this->db->password = $password;
Upvotes: 0
Reputation: 2358
I'm not sure, but i think database configuration loaded before your controller init.
You can manually connect to a database, one or more. First, save to a session, as you wrote:
configure.php (controller)
public function select_database()
{
$your_dbs = array('2010', '2011');
$db = $this->input->post('database');
if( ! in_array($db, $your_dbs))
show_error('This database not exists');
$this->session->set_userdata('selected_db', $db);
redirect('/');
}
And in your models' constructor, you can create (another) connection:
a_model.php
private $_db;
public function __construct()
{
$this->_db = $this->load->database(array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => $this->session->userdata('selected_db'),
'dbdriver' => 'mysqli',
), TRUE);
}
public function get_data()
{
return $this->_db->get('users')->result();
}
In your model you can also check if is this a valid database, with a helper forexample, in your constructor. I'm just wrote a quick sketch how I would do it.
Upvotes: 1