I need to change the database name in database.php passing value from the controller. I tried sessions and env variable. but in the database.php cannot access the value from sessions and env variable. also, i need to use the default database to load the page and when user has been logged in need to switch the database. i'm doing this for reducing size of the database data is there any solution really glad someone can help me.
databse.php
$active_group = 'default';
$query_builder = TRUE;
if($db_val==""){
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'sliate_srs',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
}
else{
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => getenv("DB_year"),
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
}
controller.php
function loginSubmit() {
$DB_year=$this->input->post('year_select');
//$this->session->unset_userdata('DB_year');
//$this->session->set_userdata('DB_year',$DB_year);
$DB_year="srs_2019";
putenv("DB_year=$DB_year");
// $_ENV["DB_year"]="srs_2019";
print_r(getenv("DB_year"));
// $DB_year="srs_2019";
$result = $this->Login_model->authenticateLogin();
if (!empty($result)) {
$now = date('Y-m-d H:i:s');
$ip = $this->input->ip_address();
$data = array(
'u_id' => $this->session->userdata('u_id'),
'u_name' => $this->session->userdata('u_name'),
'center_name' => $this->session->userdata('u_branch'),
'last_login_ip' => $this->input->ip_address('ip'),
'last_login_date_time' => $now
);
$name = $this->session->userdata('DB_year');
print_r($name);
$this->Login_model->last_login($data);
redirect('Admin/dashboard');
} else {
redirect('Login?login=invalid');
}
}
Instead of changing the config group in Database.php, why not create a custom connection in your model, as explained in Connecting with Custom Settings?
Related
I have an autoloaded DB which has all its var set in the config/database.php file as a default group:
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'dbname',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => false,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => false,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => false,
'compress' => false,
'stricton' => false,
'failover' => array(),
'save_queries' => true
);
In this default DB there is a table where I need to read an external DB, and I have all fields to get a new connection.
I need to connect both DB at the same time, but I cannot define the DB variables in the config/database.php as these are dynamic and may change depending on the DB (default) content.
My idea was this either to SET $db['external'] = [...] IN THE CONTROLLER and set the data from the default DB I read, or simply use a DNS:
Solution #1:
public function wordpress()
{
$DB = $this->load->database('default', true);
$wp_db = $DB->get_where('dbtable', ['type_needed' => 'wordpress'])->row();
$db['wp_db'] = array(
'dsn' => '',
'hostname' => $wp_db->mysql_host,
'username' => $wp_db->mysql_user,
'password' => $wp_db->mysql_password,
'database' => $wp_db->mysql_db,
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => false,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => false,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => false,
'compress' => false,
'stricton' => false,
'failover' => array(),
'save_queries' => true,
);
$this->WPDB = $this->load->database('wp_db', true);
}
Solution #1 gives "You have specified an invalid database connection group (wp_db) in your config/database.php file." error
Solution #2:
$DB = $this->load->database('default', true);
$wp_db = $DB->get_where('dbtable', ['type_needed' => 'wordpress'])->row();
$wp_dns = "mysql://$wp_db->mysql_user:$wp_db->mysql_password#$wp_db->mysql_host/$wp_db->mysql_db";
$this->WPDB = $this->load->database($wp_dns, true);
Solution #2 gives a "Invalid DB Connection String" error
Ps: I'm moving to Laravel, but this project was built with CI already :)
the only thing you've to change in your function is the following
public function wordpress()
{
$DB = $this->load->database('default', true);
$wp_db = $DB->get_where('dbtable', ['type_needed' => 'wordpress'])->row();
$arrDbData = array(
'dsn' => '',
'hostname' => $wp_db->mysql_host,
'username' => $wp_db->mysql_user,
'password' => $wp_db->mysql_password,
'database' => $wp_db->mysql_db,
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => false,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => false,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => false,
'compress' => false,
'stricton' => false,
'failover' => array(),
'save_queries' => true,
);
$this->WPDB = $this->load->database($arrDbData, true);
}
I found out Solution #2 is working but due to the dns connection which is a STRING you are to make sure the password is made of letters and numbers and NO SYMBOLS otherwise it screws up the string and does not read properly.
In my case the password was this one iidf#q0RDTh#)CrPo5PDLeVe so dashes and parenthesis created a problem where CI could not read the whole password.
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => 'tes',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => 'tes',
'dbdriver' => 'odbc',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
When I run the program there is an error:
" Message: Call to undefined method CI_DB_odbc_driver::select() Filename: models/m_city.php"
m_city.php file:
<?php
class m_city extends CI_Model
{
function get_all($where = array())
{
$this->db->select('Name,Population');
$this->db->where($where);
$this->db->limit('50');
$query = $this->db->get('City');
return $query->result_array();
}
}
I already made dsn name on odbc. the name of dsn is "tes".
If you use "Mysql" then follow the following instructions.
Please put database username and database password in the following:
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '',//Put database username
'password' => '',// put database password
'database' => 'tes',
'dbdriver' => 'odbc',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
After complete this save the page and open the page 'autoload.php' under the config folder. There search "$autoload['libraries']" option. Put the following
$autoload['libraries'] = array('database');
If you use PDO, PostgreSQL, Oracle then see the following links:
https://www.codeigniter.com/user_guide/database/configuration.html
I have tried this to make local database connection. But it gives this error:
You have specified an invalid database connection group (ci_test) in your config/database.php file.
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost', // pass this
'username' => 'root', // pass this
'password' => '', // pass this
'database' => 'ci_test', // pass this
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array());
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost', //this will be same
'username' => 'root', //replace with your username
'password' => '', //replace with your password
'database' => 'test', //replace with your database name which you want to use
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE);
try this. good luck.
This error relates to the connection group, not the database name. The connection group you have specified there is default, as shown on the first line: $db['default'].
Look for:
$active_group = 'ci_test';
Replace it with:
$active_group = 'default';
There is more information in the codeigniter documentation.
You can try this solution for your problem.
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'ci_test',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
I hope this will helps you. Thanks!
I have 2 databases, and I would like to make a query with the 2 databases, like for example
SELECT base1.table1.item1 FROM base1.table1 INNER JOIN base2.table3 ON base2.table3.item2 = base1.table1.item2 WHERE base2.table3.item4 = 'toto';
How to make this query with codeIgniter ?
I already have configured database.php in CodeIgniter with the 2 databases.
Thanks.
You can setup 2 database in config/database.php file
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'first_db',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
//set second db configuration
$db['otherdb'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'second_db',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
When you want use default database means master database
// use master dataabse
$users = $this->db->get('users');
// connect to secondary database
$otherdb = $this->load->database('otherdb', TRUE);
$data = $otherdb->get('table_name');
if your first db name is base1 and second is base2
$this->db->select('table1.item1 FROM table1');
$this->db->from('table1');
$this->db->join('base2.table3', 'base2.table3.item2 =table1.item2');
$this->where('base2.table3.item4','toto')
$query = $this->db->get();
I'm new in CI,
can someone help ?
I have 2 database connections :
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => '10.1.0.166',
'username' => 'sa',
'password' => 'Sprite12345',
'database' => 'HRD',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
$db['credit'] = array(
'dsn' => '',
'hostname' => '10.1.0.166',
'username' => 'sa',
'password' => 'Sprite12345',
'database' => 'BHAKTI',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
And now, I'm trying to change the database value of $db['credit'].
something like this :
$db2 = $this->load->database('credit', TRUE);
$db2->database = 'BIT';
echo $db2->database;
$db2 = $this->load->database('credit', TRUE);
$db2->select('*');
$db2->from('tblconfig');
$query = $db2->get()->result();
print_r($query);
But the query result still take the tblconfig from BHAKTI not from BIT.
How can I change the database value in the config/database.php ?
Thanks in advance.