Multiple Database Connection in PHP Codeigniter 3
In this post we will give you information about Multiple Database Connection in PHP Codeigniter 3. Hear we will give you detail about Multiple Database Connection in PHP Codeigniter 3And how to use it also give you demo for it if it is necessary.
Today, I want to share with you how to connect multiple databases in PHP CodeIgniter 3 application. it is very easy to configure multiple databases in CodeIgniter app. you can simply add database query, join etc with multiple databases.
As we know well, in today we may need to add multiple databases on our application. all then framework provides multiple database connections. Codeigniter also provide multiple database connections in a single app. We have to simply add database configuration array to database.php file. Then we can simple load specific database data by using “$this->load->database(‘another_db’, TRUE);” help.
Solve - fatal: Unable to create '/myproject/.git/index.lock': File exi
In this post we will give you information about Solve - fatal: Unable to create '/myproject/.git/index.lock': File exi. Hear we will give you detail about Solve - fatal: Unable to create '/myproject/.git/index.lock': File exiAnd how to use it also give you demo for it if it is necessary.
When i was working on my laravel 5 application, i did all the changes i did but when i had gone for push then i found this fatal error. It's not able to create index.lock file in .git folder. I also give full permission to .git folder and also try to remove direct index.lock file from .git directory but same error like as bellow.
Error
fatal: Unable to create '/var/www/me/theme_backend/.git/index.lock': File exists.
But at last i found how can i resolve this problem, just run bellow command and it will work.
rm -f ./.git/index.lock
Hope this code and post will helped you for implement Solve - fatal: Unable to create '/myproject/.git/index.lock': File exi. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs
Here i explain full example for add multiple connection. So you need to create two database. in this example i created two database with following name:
1)codeig
2)laravel_test
I also created “items” table with above two databases. So let’s proceed with additional configuration array.
Step 1: Add Database Configuration
In first step we will add two database configuration in database.php file. one is default and another is for extra that we need for testing. So let’s add.
application/config/database.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'codeig',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
$db['another_db'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'laravel_test',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Step 2: Add Route
In this step, we will add one route “test_db” for demo, that way when we run this route we will the output, So let’s add following route on your routes.php file.
application/config/routes.php
$route['test_db'] = 'welcome/test_db';
Step 4: Add Controller Method
In this step we require to add “test_db” method on welcome controller, So let’s add with following code. you have to just copy of welcome.php controller file:
application/controllers/Welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function test_db()
{
$this->load->database();
$query = $this->db->get("items");
echo "<pre>";
print_r($query->result());
$second_DB = $this->load->database('another_db', TRUE);
$query2 = $second_DB->get("items");
print_r($query2->result());
exit;
}
}
I hope it can help you…
Hope this code and post will helped you for implement Multiple Database Connection in PHP Codeigniter 3. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs