{"id":4325,"date":"2019-04-27T07:06:03","date_gmt":"2019-04-27T01:36:03","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=4325"},"modified":"2019-04-27T07:13:30","modified_gmt":"2019-04-27T01:43:30","slug":"how-to-load-multiple-databases-in-codeigniter-framework","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/how-to-load-multiple-databases-in-codeigniter-framework\/","title":{"rendered":"HOW TO LOAD MULTIPLE DATABASES IN CODEIGNITER FRAMEWORK."},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">HOW TO LOAD MULTIPLE DATABASES IN CODEIGNITER FRAMEWORK.<\/h4>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"mce_0\"><strong>Date posted :26\/04\/2019<\/strong><\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">LOAD MULTIPLE DATABASES:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Most importantly, codeIgniter has a config file that you can store your database connection values such as <strong>username, password, database name. <\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> Therefore this config file located at <strong>application\/config\/database.php.<\/strong>  <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$db['default'] = array(\n\t'dsn'\t=> '',\n\t'hostname' => 'localhost',\n\t'username' => 'root',\n\t'password' => '',\n\t'database' => 'form',\n\t'dbdriver' => 'mysqli',\n\t'dbprefix' => '',\n\t'pconnect' => FALSE,\n\t'db_debug' => (ENVIRONMENT !== 'production'),\n\t'cache_on' => FALSE,\n\t'cachedir' => '',\n\t'char_set' => 'utf8',\n\t'dbcollat' => 'utf8_general_ci',\n\t'swap_pre' => '',\n\t'encrypt' => FALSE,\n\t'compress' => FALSE,\n\t'stricton' => FALSE,\n\t'failover' => array(),\n\t'save_queries' => TRUE\n);\n<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>hostname<\/strong> &#8211; The hostname of your database server. Often this is &#8220;localhost&#8221;.<\/li><li><strong>username<\/strong>&nbsp;&#8211; The username used to connect to the database.<\/li><li><strong>password<\/strong>&nbsp;&#8211; The password used to connect to the database.<\/li><li><strong>database<\/strong>&nbsp;&#8211; The name of the database you want to connect to.<\/li><li><strong>dbdriver<\/strong> &#8211; The database type. ie: mysql, postgres,etc. Must be specified in lower case.<\/li><li><strong>dbprefix<\/strong> &#8211; An optional table prefix which will added to the table name when running&nbsp;&nbsp;queries of Active Record. This permits multiple CodeIgniter installations to share one database.<\/li><li><strong>pconnect<\/strong> &#8211; TRUE\/FALSE  &#8211; Whether to use a persistent connection.<\/li><li><strong>db_debug<\/strong> &#8211; TRUE\/FALSE  &#8211; Whether database errors should be displayed.<\/li><li><strong>cache_on<\/strong> &#8211; TRUE\/FALSE  &#8211; Whether database query caching is enabled.<\/li><li><strong>cachedir<\/strong>&nbsp;&#8211; The absolute server path to your database query cache directory.<\/li><li><strong>char_set<\/strong>&nbsp;&#8211; The character set used in communicating with the database.<\/li><li><strong>dbcollat<\/strong>&nbsp;&#8211; The character collation used in communicating with the database.<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">DATABASE CONFIGURATION FILE OF CODEIGNITER<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Similarly, to open Database Configuration file,<strong> go to <\/strong><em><strong>application\/config\/database.php<\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Firstly,  you need to change <strong>&#8216;pconnect&#8217; =&gt; FALSE,<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">MAKE TWO DATABASE CONNECTIVITY SETTINGS<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* ======= First database connectivity Settings =========*\/\n$db['default'] = array(\n\t'dsn'\t=> '',\n\t'hostname' => 'localhost',\n\t'username' => 'root',\n\t'password' => '',\n\t'database' => 'form',\n\t'dbdriver' => 'mysqli',\n\t'dbprefix' => '',\n\t'pconnect' => TRUE,\n\t'db_debug' => (ENVIRONMENT !== 'production'),\n\t'cache_on' => FALSE,\n\t'cachedir' => '',\n\t'char_set' => 'utf8',\n\t'dbcollat' => 'utf8_general_ci',\n\t'swap_pre' => '',\n\t'encrypt' => FALSE,\n\t'compress' => FALSE,\n\t'stricton' => FALSE,\n\t'failover' => array(),\n\t'save_queries' => TRUE\n);\n<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* ======= Second database connectivity Settings =========*\/\n$db['database2'] = array(\n\t'dsn'\t=> '',\n\t'hostname' => 'localhost',\n\t'username' => 'root',\n\t'password' => '',\n\t'database' => 'db2',\n\t'dbdriver' => 'mysqli',\n\t'dbprefix' => '',\n\t'pconnect' => TRUE,\n\t'db_debug' => (ENVIRONMENT !== 'production'),\n\t'cache_on' => FALSE,\n\t'cachedir' => '',\n\t'char_set' => 'utf8',\n\t'dbcollat' => 'utf8_general_ci',\n\t'swap_pre' => '',\n\t'encrypt' => FALSE,\n\t'compress' => FALSE,\n\t'stricton' => FALSE,\n\t'failover' => array(),\n\t'save_queries' => TRUE\n);\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">NOTE:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Important Setting:<\/strong> As a result, codeigniter cannot connect multiple database in connection. So you need to put a <strong>simple setting<\/strong> in <strong>system\/database\/ DB_driver.php<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\npublic function simple_query($sql)\n {\n if ( ! $this->conn_id)\n {\n if ( ! $this->initialize())\n {\n return FALSE;\n }\n }\n $this->db_select(); \/\/Just Add this line here\n \n return $this->_execute($sql);\n } \n \n?><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, I have discussed the problem and the solution to using multiple database connections in CodeIgniter projects. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks for using&nbsp;<a href=\"https:\/\/pheonixsolutions.com\/\">pheonix solutions<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You find this tutorial helpful? Share with your friends to keep it alive.<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>HOW TO LOAD MULTIPLE DATABASES IN CODEIGNITER FRAMEWORK. Date posted :26\/04\/2019 LOAD MULTIPLE DATABASES: Most importantly, codeIgniter has a config [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[605,632],"class_list":["post-4325","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-codeigniter-framework","tag-load-multiple-databases","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-17L","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4325","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=4325"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4325\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=4325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=4325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=4325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}