In Applications/config folder open the database.php file.
You will find the default database connection code. Which is need to to be
replaced with your settings. Like database name, hostname, username, password,
etc… And ‘$active_group’ variable is set with ‘default’ value. You can replace
them all but only if you are sure about them. The ‘Record set’ code would be like this,
$active_group = 'default';
$active_record
= TRUE;
$db['default']['hostname']
= 'localhost';
$db['default']['username']
= 'root';
$db['default']['password']
= '';
$db['default']['database']
= 'feedback';
$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;
If you want to connect to multiple databases at a time, you
can connect by Copying Record set and paste below the code and change the multi-dimensional
array key value ‘default’ to ‘new_conn’ i.e
$db['default'] ==> $db['comments’]
And as well as the database values. Like hostname, dbname,
username, pwd, …
Following is the new database connection array.
//================NEW COMMENTS
DATABASE ==========
$db['comments']['hostname']
= 'localhost';
$db['comments']['username']
= 'root';
$db['comments']['password']
= '';
$db['comments']['database']
= 'comments';
$db['comments']['dbdriver']
= 'mysql';
$db['comments']['dbprefix']
= '';
$db['comments']['pconnect']
= TRUE;
$db['comments']['db_debug']
= TRUE;
$db['comments']['cache_on']
= FALSE;
$db['comments']['cachedir']
= '';
$db['comments']['char_set']
= 'utf8';
$db['comments']['dbcollat']
= 'utf8_general_ci';
$db['comments']['swap_pre']
= '';
$db['comments']['autoinit']
= TRUE;
$db['comments']['stricton']
= FALSE;
Connection to
new database:
By default the CodeIgniter connection statement is
$this->load->database();
This code connects the default
database which is set following variables,
$active_group = 'default';
$active_record = TRUE;
For connecting to new database you need to specify the
database name along with this default connection statement. And you have to set
TRUE for $active_record variable, Following is the code snippet:
$a=$this->load->database('comments',TRUE);
First database() method arguments is database connection
array key value which will be set to ‘$active_group’ I.e comments which we
already defined below the default database connection array. And second argument
is TRUE/FALSE which will change ‘$active_record’ variable value.
This connection statement returns the connection results
which we storing into a variable. So we can query the database with that
variable. Following code is example query statements,
$res=$a->insert("comment ",$data);
$a->_error_message();
$a->distinct();
$a->select('name');
$query=$a->get("comment");
…
You may wish to connect to another new database then follow
the same steps to achieve.
thank s it was very usefull for me .
ReplyDeleteYou're welcome majid..
DeleteYour Blog is very helpful,Click here to Seeinsert data into database in Codeigniter
ReplyDeleteFramework only give the final structure and look for the website with complete mode.I really grateful to getting this information from the blog.
ReplyDeleteWeb Designing Company Bangalore | Website Designers Bangalore
hey nice source for us, and you have incredible work in this blog for collection the information and i have to bookmark this blog for the future use.
ReplyDeletePHP Web Development Services India
This information gives the newbees an idea to connect various databases through CI. As php web application development services opens the door for fast and accurate website development, there is a lot of need to connect various databses to fetch the information. You informations helps us a lot, Thanks.
ReplyDeletedsg
ReplyDeleteIf you want to connect multiple database in Codeigniter application please follow below link and configure multiple database setting.
ReplyDeletehttp://phpwebschool.com/how-to-connect-multiple-database-in-codeigniter/
Thanks for sharing such a useful and informative post.
ReplyDeleteE-commerce solution provider
Ahaa, its nice conversation on the topic of Connecting multiple db using CodeIgniter framework at this weblog, I have read all that, so at this time me also commenting at this place. Thanks for sharing helpful info with us.
ReplyDeleteResponsive Kids Websites Design and Marketing in Affordable Price by OptimiZed360.Com
Thank you for this golden advice you shared here.Thank you for such a great article :)
ReplyDeleteMobile App Development Company Indore
This comment has been removed by the author.
ReplyDeleteGreat tip. However, it should also be explained that developers should make it a practice to make sure to close the database connection manually once the code doesn't require the connection anymore. source: https://www.cloudways.com/blog/connect-multiple-databases-codeigniter/
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteNice information for me, thanks for sharing this details and i read the whole information and i will bookmark this information for future use.
ReplyDeleteDedicated Codeigniter Developer
This comment has been removed by the author.
ReplyDeleteThanks for sharing such a great blog Keep posting.
ReplyDeletesales automation process
marketing automation software
buy database for marketing
corporate directory
Best chauffeur service in Victoira
ReplyDeleteSilver Executive Cab is a Chauffeur Cars company in Melbourne specifically company targets Melbourne Airport transfers, Corporate transfers, Regular Pick-ups, Wedding Hire Cars & private hire cars. It is an essay way to contact at Silver Executive Cab & you will get proper feedback or support by our technical executives. So Don't waste your time call or Book online your luxury car in competitive rates .
Great Article,
ReplyDeleteThis blog site has lots of really useful info on it! Cheers for informing me! for web development
This comment has been removed by the author.
ReplyDelete