Welcome Guest, Not a member yet? Register   Sign In
Configure database config from function
#1

Hi!

Is possible to set database config from normal form?
Normal config has this option - ok, but now I need to set database config (hostname, user, password, etc.)
Reply
#2

(This post was last modified: 02-29-2016, 08:38 AM by keulu.)

yes you can

in your database config file add as many configuration groups as the numbers of your databases:
PHP Code:
$db['a']['hostname'] = 'localhost';
$db['a']['username'] = 'user';
$db['a']['password'] = 'pw';
$db['a']['database'] = 'db1';
...

$db['b']['hostname'] = 'localhost';
$db['b']['username'] = 'user';
$db['b']['password'] = 'pw';
$db['b']['database'] = 'db2';
...

//set the default db
$active_group 'a'

then on your model initialize a class variable:
PHP Code:
private $db_b

and, into the contructor, set it as follow
PHP Code:
__construct()
{
   ...
   
$this->db_b $this->load->database('b'TRUE); 


now you are able to use the database b as usual:
PHP Code:
$this->db_b->query('YOUR QUERY'); 

and obviously the default one as follow:
PHP Code:
$this->db->query('YOUR QUERY'); 

Make sure to set the persistant connect to false
PHP Code:
$db['a']['pconnect'] = FALSE;
$db['b']['pconnect'] = FALSE
Reply
#3

(02-29-2016, 08:36 AM)keulu Wrote: yes you can

in your database config file add as many configuration groups as the numbers of your databases:
PHP Code:
$db['a']['hostname'] = 'localhost';
$db['a']['username'] = 'user';
$db['a']['password'] = 'pw';
$db['a']['database'] = 'db1';
...

$db['b']['hostname'] = 'localhost';
$db['b']['username'] = 'user';
$db['b']['password'] = 'pw';
$db['b']['database'] = 'db2';
...

//set the default db
$active_group 'a'

then on your model initialize a class variable:
PHP Code:
private $db_b

and, into the contructor, set it as follow
PHP Code:
__construct()
{
   ...
   
$this->db_b $this->load->database('b'TRUE); 


now you are able to use the database b as usual:
PHP Code:
$this->db_b->query('YOUR QUERY'); 

and obviously the default one as follow:
PHP Code:
$this->db->query('YOUR QUERY'); 

Make sure to set the persistant connect to false
PHP Code:
$db['a']['pconnect'] = FALSE;
$db['b']['pconnect'] = FALSE

You don't understand - I know how to use multiple databases. I need to set config this config manualy, because I want to create installer for my APP
Reply
#4

arf okay, my bad,

yes you can by overwriting the config file.

in php you can write files
http://php.net/manual/fr/function.fwrite.php

you just have to remove the old database config and rewrite the new one Smile
Reply
#5

You can pass array of patamteres to database loader like this:

PHP Code:
$config['hostname'] = 'localhost';
$config['username'] = 'myusername';
$config['password'] = 'mypassword';
$config['database'] = 'mydatabase';
$config['dbdriver'] = 'mysqli';
$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); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB