⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

[ANSWERED] Can I switch databases?



jcasanova
  • profile picture
  • Member

Posted 08 May 2012 - 17:11 PM

Hi,

Is possible to reading tables from 1 db then switch to a second db in the same application?

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 08 May 2012 - 18:28 PM

If you mean at the same controller yes.

You can do something like this:


function example1()
{
$this->db = $this->load->database('db1',true);
$crud = new grocery_CRUD();
$crud->set_table('customers');
$crud->columns('customerName','phone','addressLine1','creditLimit');

$output = $crud->render();

$this->_example_output($output);
}
function example2()
{
$this->db = $this->load->database('db2',true);
$crud = new grocery_CRUD();
$crud->set_table('customers');
$crud->columns('customerName','phone','addressLine1','creditLimit');

$output = $crud->render();

$this->_example_output($output);
}


where the db1/db2 is the name of your group for example:


$db['db1']['hostname'] = 'localhost';
$db['db1']['username'] = "root";
$db['db1']['password'] = "";
$db['db1']['database'] = 'my_database_1';
$db['db1']['dbdriver'] = 'mysql';
$db['db1']['dbprefix'] = '';
$db['db1']['pconnect'] = FALSE;
$db['db1']['db_debug'] = TRUE;
$db['db1']['cache_on'] = FALSE;
$db['db1']['cachedir'] = '';
$db['db1']['char_set'] = 'utf8';
$db['db1']['dbcollat'] = 'utf8_general_ci';
$db['db1']['swap_pre'] = '';
$db['db1']['autoinit'] = TRUE;
$db['db1']['stricton'] = FALSE;

$db['db2']['hostname'] = 'localhost';
$db['db2']['username'] = "root";
$db['db2']['password'] = "";
$db['db2']['database'] = 'my_database_2';
$db['db2']['dbdriver'] = 'mysql';
$db['db2']['dbprefix'] = '';
$db['db2']['pconnect'] = FALSE;
$db['db2']['db_debug'] = TRUE;
$db['db2']['cache_on'] = FALSE;
$db['db2']['cachedir'] = '';
$db['db2']['char_set'] = 'utf8';
$db['db2']['dbcollat'] = 'utf8_general_ci';
$db['db2']['swap_pre'] = '';
$db['db2']['autoinit'] = TRUE;
$db['db2']['stricton'] = FALSE;


This is your config at application/config/database.php

jcasanova
  • profile picture
  • Member

Posted 08 May 2012 - 18:36 PM

Excellent i'll test it asap

Thanks

duangsin.k
  • profile picture
  • Member

Posted 25 May 2012 - 08:05 AM

Excellent

stavgian
  • profile picture
  • Member

Posted 19 September 2013 - 09:04 AM

Is it possible to have relation with other database table field? Can this be done with custom model?


davidoster
  • profile picture
  • Member

Posted 20 September 2013 - 19:23 PM

Hello [member=stavgian].

Using a custom model, either one that extends the GC model or one that extends the base CI_Model, it is possible to have such relations.

It just needs carefull thought how to incorporate within your controller.


siarik
  • profile picture
  • Member

Posted 03 October 2013 - 04:31 AM

I am using version grocerycrud 1.4.1
 
I am trying to connect multiple database

 

 
config/database.php
 
 
*/
 
//$active_group = 'default';
//$active_record = TRUE;
 
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'xxx';
$db['default']['database'] = 'db01';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
//$db['default']['pconnect'] = TRUE;
$db['default']['pconnect'] = FALSE;
$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;
 
 
$db['db2']['hostname'] = '192.168.1.85';
$db['db2']['username'] = 'root';
$db['db2']['password'] = 'xx';
$db['db2']['database'] = 'db02';
$db['db2']['dbdriver'] = 'mysql';
$db['db2']['dbprefix'] = '';
//$db['db2']['pconnect'] = TRUE;
$db['db2']['pconnect'] = FALSE;
$db['db2']['db_debug'] = TRUE;
$db['db2']['cache_on'] = FALSE;
$db['db2']['cachedir'] = '';
$db['db2']['char_set'] = 'utf8';
$db['db2']['dbcollat'] = 'utf8_general_ci';
$db['db2']['swap_pre'] = '';
$db['db2']['autoinit'] = TRUE;
$db['db2']['stricton'] = FALSE;
 
 
/* End of file database.php */
/* Location: ./application/config/database.php */
 
 
public function sms()
        {
            $this->db = $this->load->database('db2',true);
 
            $crud = new grocery_CRUD();
 
            $crud->set_theme('datatables');
            $crud->set_table('inbox');
            $crud->set_subject('SMS');
 
            $crud->columns('inbox');
            $crud->fields('inbox');
 
            $output = $crud->render();
 
            $this->v_list_sms($output);
        }
 
and after I try the result is

 

A Database Error Occurred

Unable to connect to your database server using the provided settings.

Filename: C:\xampp\htdocs\pusink\system\database\DB_driver.php

Line Number: 124

 

is there something wrong with the settings
 
please help me..

 

 


davidoster
  • profile picture
  • Member

Posted 11 October 2013 - 04:38 AM

 

I am using version grocerycrud 1.4.1
 
I am trying to connect multiple database

 

 
config/database.php
 
 
*/
 
//$active_group = 'default';
//$active_record = TRUE;
 
 
is there something wrong with the settings
 
please help me..

 

 

 

did you really remark the line //$active_record = TRUE;

This is needed actually!


hurtz
  • profile picture
  • Member

Posted 01 March 2017 - 02:34 AM

I tried this solution but it says "Fatal error: Uncaught exception 'Exception' with message 'The table name does not exist. Please check you database and try again.' in D:\wamp\www\ATS\admin\application\libraries\grocery_crud.php on line 4349"

 

here is my config/database

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'logbook';
$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;
$db['default']['failover'] = array();

$db['db2']['hostname'] = 'localhost';
$db['db2']['username'] = 'root';
$db['db2']['password'] = '';
$db['db2']['database'] = 'calendar';
$db['db2']['dbdriver'] = 'mysql';
$db['db2']['dbprefix'] = '';
$db['db2']['pconnect'] = TRUE;
$db['db2']['db_debug'] = TRUE;
$db['db2']['cache_on'] = FALSE;
$db['db2']['cachedir'] = '';
$db['db2']['char_set'] = 'utf8';
$db['db2']['dbcollat'] = 'utf8_general_ci';
$db['db2']['swap_pre'] = '';
$db['db2']['autoinit'] = TRUE;
$db['db2']['stricton'] = FALSE;
$db['db2']['failover'] = array();

and this is my controller

    function event()
    {
        
        $this->db = $this->load->database('db2',true);
        
        $crud = new grocery_CRUD();
        
        $crud->set_table('tbl_event');
        $crud->set_subject('Calendar')
             ->columns('time_date','event_name','divID','venue')
             ->display_as('event_name','Event Name')
             ->display_as('time_date','Time & Date')
             ->display_as('time_start','Start time')
             ->display_as('time_end','End time')
             ->display_as('date_start','Start date')
             ->display_as('date_end','End date')
             ->display_as('venue','Venue')
             ->display_as('address1','Name of Venue')
             ->display_as('address2','Address 1')
             ->display_as('address3','Address 2')
             ->display_as('divID','Division Concerned');
        $crud->fields('event_name','date_time','time_start','time_end','date_start','date_end','venue','address1','address2','address3','divID');

        $crud->set_relation('divID','division','description');
        
        
        $crud->callback_before_insert(array($this,'set_time_date_value'));
        $crud->callback_before_update(array($this,'set_time_date_value'));
        
        $crud->order_by('date_start','asc');

        $output = $crud->render();
        
        $this->_pg_view($output);
    }