Welcome Guest, Not a member yet? Register   Sign In
A Permanent On-the-fly Database Connection
#1

[eluser]kaege[/eluser]
Hello Smile

I've been making a web application to make some reports in a government's agency. The app is to be installed in the server first so the database configuration should be set by the user on installation.
I make a config file named custom_db.php to store the configuration in DSN format. The mechanism to store user's database configuration is like this:

Code:
// ./application/config/custom_db.php

$config['dsn']='';

Code:
// A controller

$dbdriver=$this->input->post('dbdriver');
$username=$this->input->post('username');
$password=$this->input->post('password');
$hostname=$this->input->post('hostname');
$database=$this->input->post('database');

$this->config->load('custom_db');
$this->input->set_item('dsn', "$dbdriver://$username:$password@$hostname/$database");
$this->load->database($this->config->item('dsn'));

It worked, but the connection ($config['dsn']) isn't permanent. After I changed page, $config['dsn'] value return to empty string. I know that I could just make a new database configuration file on the fly, But I've been thinking of another way to do so via Config class. Any suggestion or perhaps correction?
#2

[eluser]monkeyhouse[/eluser]
Unless I'm mistaken, the Config class does not support writing to the config files. The code for the config class is fairly short.

It could be extended to save cfg files, but it might be easier to utilize a central db to store the settings and draw the config from that, that would allow you to save configs on a per user basis anyway... Unless you're creating a simple installer, then it'd just be simpler to dup and write out the config.
#3

[eluser]jgetner[/eluser]
where do you get this method from?

Code:
$this->input->set_item('dsn', "$dbdriver://$username:$password@$hostname/$database");
#4

[eluser]InsiteFX[/eluser]
Code:
$this->config->set_item('dsn', 'dsn item string');

Of course you would need to create the config item first!

InsiteFX
#5

[eluser]kaege[/eluser]
[quote author="jgetner" date="1303438866"]where do you get this method from?

Code:
$this->input->set_item('dsn', "$dbdriver://$username:$password@$hostname/$database");
[/quote]

Sorry. It's a mistake. The code should be:
Code:
$this->config->set_item('dsn', "$dbdriver://$username:$password@$hostname/$database");

Because Config class couldn't write a file, I has decided to manually write the config file using write_file() from File helper. And it causes another problem. i can't post it here because it doesn't match the topic.




Theme © iAndrew 2016 - Forum software by © MyBB