views:

45

answers:

2

Hello,

How do i log inside database with separate passwords? We can configure only one user in database.php. I want that the login process should happen with different mysql username and password because that table is protected.

Thanks in advance:)

+2  A: 

By following the user documentation you will find this page: http://codeigniter.com/user_guide/database/connecting.html

The documentation states that you can connect to multiple databases by specifying a group. If you look in your database.php file, you will see that the connection arrays is formatted like this:

$db['default']['hostname'] = "localhost";

The 'group' here is default, which is loaded the way you always have been doing:

$this->load->database();

When you need to connect to another database, specify a new group:

$db['my_secret_db']['hostname'] = "localhost";
$db['my_secret_db']['username'] = "other_mysql_user";
...

And you load it like so:

$MyOriginalDb = $this->load->database('default', true);
$MyOtherDb = $this->load->database('my_secret_db', true);

By loading these connections into their own objects, you will now use:

$MyOtherDb->query();

instead of

$this->db->query();

I hope this helps.

Repox
+2  A: 

create another connection group in your database.php

$active_group = "post";
$active_record = TRUE;

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "user";
$db['default']['password'] = "user-only";
$db['default']['database'] = "something";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$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['post']['hostname'] = "localhost";
$db['post']['username'] = "admin";
$db['post']['password'] = "admin-only";
$db['post']['database'] = "something";
$db['post']['dbdriver'] = "mysql";
$db['post']['dbprefix'] = "";
$db['post']['pconnect'] = TRUE;
$db['post']['db_debug'] = TRUE;
$db['post']['cache_on'] = FALSE;
$db['post']['cachedir'] = "";
$db['post']['char_set'] = "utf8";
$db['post']['dbcollat'] = "utf8_general_ci";

create another two model files. one file load the 'default' configuration, and the other load the 'post' configuration.

eg:

$DB1 = $this->load->database('default', TRUE);
$DB2 = $this->load->database('post', TRUE);
ariawan