0

Can we change arguments like host-name, user-name, password and database dynamically or we must go to database.php to change it.

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '****';
$db['default']['database'] = 'database';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';

Is there any possibility to change them dynamically?

1
  • Are you trying to change database connections within different portions of the same site or just change connections based on site environment? Commented Aug 21, 2015 at 18:43

1 Answer 1

1

I think something like this would work, in a model or Library.

$dynamic = array(
  'hostname' => 'localhost',
  'username' => 'root',
  'password' => '*****',
  'database' => 'database'
);
$test['hostname'] = $dynamic['hostname'];
$test['username'] = $dynamic['username'];
$test['password'] = $dynamic['password'];
$test['database'] = $dynamic['database'];
$test['dbdriver'] = 'mysqli';
$test['dbprefix'] = '';
$test['pconnect'] = FALSE;
$test['db_debug'] = TRUE;
$test['cache_on'] = FALSE;
$test['cachedir'] = '';
$test['char_set'] = 'utf8';
$test['dbcollat'] = 'utf8_general_ci';

$test_db = $this->load->database($test, true);

Then you would access this database, like this;

$test_db->get('users');

Where are you planning on getting the $dynamic settings from?

Sign up to request clarification or add additional context in comments.

2 Comments

i want to make an form for database setting, instead of going in database.php to configer
The code I posted will connect to a database, using the details in the $dynamic array. Where you get this data from, is up to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.