Codeigniter: Different methods with different database connection (multiple database connection)
You need to define variable outside you constructor
public $DB1;
public $DB2;
And call it inside constructor as
function __construct()
{
parent::__construct();
$this->DB1= $this->load->database("db1", TRUE);
$this->DB2= $this->load->database("db2", TRUE);
}
And inside your function
public function get_from_db1($id)
{
// $query = $this->db->get_where("my_table1",array("ID"=>$id));
$query = $this->DB1->get_where("my_table1", array("ID" => $id));
return $query->row_array();
}
public function get_from_db2($id)
{
// $query = $this->db->get_where("my_table2",array("ID"=>$id));
$query = $this->DB2->get_where("my_table2", array("ID" => $id));
return $query->row_array();
}
Codeigniter - best way to use two different database
I'm not sure if you call this "best" way, but a way, as described in the tutorial, is this,
in the database file, you have the default configuration, a part of which is:
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "user";
$db['default']['password'] = "database";
$db['default']['database'] = "db1";
now you can create another group, say we call it group1 and we want it to have everything the same as the default database settings except for the name, so you can do
$db['group1']=$db['default'];
$db['group1']['database']="db2";
then, when you want to use the second database, just go
$DB2 = $this->load->database('group1', TRUE);
and then, instead of $this->db->foo()
, you will do $DB2->foo()
alternatively (as suggested in comments by sbaaaang), you can do $this->db=$DB2;
to keep everything the same
and you can extend this to multiple groups like this
$DB1 = $this->load->database('group1', TRUE);
$DB2 = $this->load->database('group2', TRUE);
...
$DBn = $this->load->database('groupn', TRUE);
CodeIgniter loading from multiple controllers and models
In most cases you're going to have one model per database table that handles all the database functionality for that particular table. When it comes to controllers the opinions differ here, I know some people that have one controller per view, personally I make my controllers functionality specific. So if I am dealing with membership functions they all go in the membership controller, sales functions go in a sales controller and so on.
If you have functions that a lot of controllers are going to use repeatedly look at creating a MY_Controller and extending the base controller with it. Basically you create a controller that extends the base CI controller and then all your other controllers extend that giving all your controller the functionality that is in MY_Controller.
Read more here: http://ellislab.com/codeigniter/user-guide/general/core_classes.html
How to connect with user specified database in codeigniter
According to the guide, you can manually pass database connectivity settings via the third parameter of $this->load->model
:
$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$this->load->model('Model_name', '', $config);
// or as gorelative notes, to access multiple databases:
$DB2 = $this->load->database($config, TRUE);
Unable to connect model page from controller page in code igniter
This is how you load model from your controller:
public function __construct()
{
parent::__construct();
$this->load->model('models', 'your_model'); //use 'your_model' to reference your model
}
This is how you called your model:
$this->your_model->dbdata($data);
//'your_model' is from constructor, 'dbdata' is the name of your function in model
Then make sure the method accept the 'data':
public function dbdata($data)
{
//your code goes here
}
Related Topics
Expose "Use" Classes to Included File
PHP Regex to Remove Http:// from String
Mssql Query Issue in PHP and Querying Text Data
What Is the Concept of Service Container in Laravel
What Are the Risks of PHP Sessions
Codeigniter Back Button After Logout
PHP Exec() Is Not Executing the Command
Phpexcel_Writer_Exception with Message "Could Not Close Zip File PHP://Output."
What Is the Overhead of Using PHP Int
Cross Domains Sessions - Shared Shopping Cart Cross Domains
Simple Form Not Sending Data via _Post
Extract Url's from a String Using PHP
PHP Warning:Mysqli_Num_Rows() Expects Parameter 1 to Be MySQLi_Result, Object Given
PHP "Pretty Print" JSON_Encode
A Restful Persistence Solution Usable with Backbone.Js... in PHP
Conditional Switch Statements in PHP