Sunday, January 27, 2013

Connecting multiple db using CodeIgniter framework:


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.




20 comments:

  1. thank s it was very usefull for me .

    ReplyDelete
  2. Framework only give the final structure and look for the website with complete mode.I really grateful to getting this information from the blog.
    Web Designing Company Bangalore | Website Designers Bangalore

    ReplyDelete
  3. 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.

    PHP Web Development Services India

    ReplyDelete
  4. 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.

    ReplyDelete
  5. If you want to connect multiple database in Codeigniter application please follow below link and configure multiple database setting.

    http://phpwebschool.com/how-to-connect-multiple-database-in-codeigniter/

    ReplyDelete
  6. Thanks for sharing such a useful and informative post.
    E-commerce solution provider

    ReplyDelete
  7. 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.

    Responsive Kids Websites Design and Marketing in Affordable Price by OptimiZed360.Com

    ReplyDelete
  8. Thank you for this golden advice you shared here.Thank you for such a great article :)
    Mobile App Development Company Indore

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Great 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/

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. Nice information for me, thanks for sharing this details and i read the whole information and i will bookmark this information for future use.

    Dedicated Codeigniter Developer

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. Best chauffeur service in Victoira
    Silver 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 .

    ReplyDelete
  15. Great Article,
    This blog site has lots of really useful info on it! Cheers for informing me! for web development

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete