• 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

#11
[eluser]dev.bashar[/eluser]
Code:
<?php
class user_model extends CI_Model {
    
        public function test()
        {
        $specific['hostname'] = "localhost";
        $specific['username'] = "myusername";
        $specific['password'] = "mypassword";
        $specific['database'] = "mydatabase";
        $specific['dbdriver'] = "mysql";
        $specific['dbprefix'] = "";
        $specific['pconnect'] = FALSE;
        $specific['db_debug'] = TRUE;
        $specific['cache_on'] = FALSE;
        $specific['cachedir'] = "";
        $specific['char_set'] = "utf8";
        $specific['dbcollat'] = "utf8_general_ci";
        
        $this->load->database($specific);
        
        //get data
        $o = new db_user();
        $array = array();
        $o->where($array)->get();
        echo $num_rows = $o->result_count();
        }
}
?>

this is my test code

when i call function from controller


Code:
public function calldatabase()
{  
     $user_id = $this->user_model->test();
}


it's still connect with main database
and it's didn't print wrong database connections ...


can you help me please Smile

#12
[eluser]Matalina[/eluser]
Try this:
Code:
<?php
class user_model extends CI_Model {
    
        public function test()
        {
        $specific['hostname'] = "localhost";
        $specific['username'] = "myusername";
        $specific['password'] = "mypassword";
        $specific['database'] = "mydatabase";
        $specific['dbdriver'] = "mysql";
        $specific['dbprefix'] = "";
        $specific['pconnect'] = FALSE;
        $specific['db_debug'] = TRUE;
        $specific['cache_on'] = FALSE;
        $specific['cachedir'] = "";
        $specific['char_set'] = "utf8";
        $specific['dbcollat'] = "utf8_general_ci";
        
        $db = $this->load->database($specific,TRUE);
        
        $db->get('table_name');
        }
}
?>

I use multipel databases on a daily basis and this is how I do it: http://ellislab.com/forums/viewthread/215036/

#13
[eluser]dev.bashar[/eluser]
thank you Matalina for the reply

i found the solution and it's like that

but now i have another problem Smile

i want to disable all database default errors
and i set
Code:
$specific['db_debug'] = FALSE;

but now how i can to create custom message if the database on the second connect is wrong?
and must load the other page content


thanks for all


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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