• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CodeIgniter how to connect multi database using database.php for first connection and Model for second connection

#1
[eluser]dev.bashar[/eluser]
Hello

I have many database connection
i have primary connection in the database.php file


$active_group = 'default';
$active_record = TRUE;

Code:
$db['default']['hostname'] = 'xxx';
$db['default']['username'] = 'xxx';
$db['default']['password'] = 'xxxx';
$db['default']['database'] = 'xxx';
$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['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

and i have many more connections ..
but i must to config another connections using Model

can i pass parameters to database.php?

or can i put like this code in Model


Code:
$db['xxx']['hostname'] = 'xxx';
$db['xxx']['username'] = 'xxx';
$db['xxx']['password'] = 'xxxx';
$db['xxx']['database'] = 'xxx';
$db['xxx']['dbdriver'] = 'mysql';
$db['xxx']['dbprefix'] = '';
$db['xxx']['pconnect'] = TRUE;
$db['xxx']['db_debug'] = TRUE;
$db['xxx']['cache_on'] = FALSE;
$db['xxx']['cachedir'] = '';
$db['xxx']['char_set'] = 'utf8';
$db['xxx']['dbcollat'] = 'utf8_general_ci';
$db['xxx']['swap_pre'] = '';
$db['xxx']['autoinit'] = TRUE;
$db['xxx']['stricton'] = FALSE;

$this->load->database('xxx', TRUE);


#2
[eluser]zay yar phone[/eluser]
Yes, You can load new database connection as follows, and assign to variable.

Code:
$remote = $this->load->database(‘xxx’, TRUE);
$Q = $remote->query('select * from table');
$results = $Q->result_array();



#3
[eluser]dev.bashar[/eluser]
no i have 100 database and i want to connect from the Model for this database
not from the database.php

only the main database in from database.php

#4
[eluser]zay yar phone[/eluser]
do you mean 1 Model -> 1 database, Where do you get the database name dynamically.
datbase name same as model name?'
Other database configurations for 100 database are same, such as (hostname,username,password)?

#5
[eluser]dev.bashar[/eluser]
no i mean

i have main database
and in this main database there is the configuration settings to another database
i have some project that every user have different database ..

now the main database configuration in database.php

but now i must to check the user login From model and connect to his database

thank you for help Smile

#6
[eluser]zay yar phone[/eluser]
If I understand you correctly
1) database configuration settings for 100 databases are stored in the main database.
2) Users are authenticated using the main database.
3)Once the users are authenticated you have to figure out the their respective database to connect to.

In the authentication process store the user specific database name into the PHP
$_SESSION['db_name'] = 'db1'; // db1 is the databasename

Write the My_Model which extends the CI_Model. Store MY_Model into the ./applications/core/
In the Constructor of My_Model class establish the database connection dynamically.

Code:
$specific['hostname'] = 'xxx';
$specific['username'] = 'xxx';
$specific['password'] = 'xxxx';
$specific['database'] = $_SESSION['db_name']; // this is the db name
$specific['dbdriver'] = 'mysql';
$specific['dbprefix'] = '';
$specific['pconnect'] = TRUE;
$specific['db_debug'] = TRUE;
$specific['cache_on'] = FALSE;
$specific['cachedir'] = '';
$specific['char_set'] = 'utf8';
$specific['dbcollat'] = 'utf8_general_ci';
$specific['swap_pre'] = '';
$specific['autoinit'] = TRUE;
$specific['stricton'] = FALSE;
$this->load->database($specific);

All finally all your Model class extends the MY_Model.
Code:
// in certain case which you want to connect to the main database in between
$this->load->database(); // without any parameter

#7
[eluser]dev.bashar[/eluser]
it's not work

i see the manual connect in CI docs

but it's not working
must i call any function to switch between tow database?


thank you for help again
but i am really need this code

#8
[eluser]dev.bashar[/eluser]
i insert this code in my Model

but he didn't print any error message and the connection still in main database

#9
[eluser]zay yar phone[/eluser]
These will help you debug,
Code:
// 1) call this after the user is logged-in. Check if the $_SESSION['db_name'] is stored properly.
echo '<pre>';
print_r($_SESSION);
echo '</pre>';

// 2) Make sure your MY_Model is store in ./application/core. And its __construct get called.


class MY_Model extends CI_Model
{


public function __construct()
{
parent::__construct();

$specific['hostname'] = 'xxx';
.
.
.
$specific['stricton'] = FALSE;

$this->load->database($specific);
}

}

// 3) try calling those database using hard coded values

#10
[eluser]zay yar phone[/eluser]
can you send me the code of MY_Model.php which is stored in the ./application/core.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.