Jump to content


[ANSWERED] Can I switch databases?


  • Please log in to reply
8 replies to this topic

#1 jcasanova

jcasanova

    Advanced Member

  • Members
  • PipPipPip
  • 61 posts

Posted 08 May 2012 - 05:11 PM

Hi,

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

#2 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,099 posts
  • LocationLondon

Posted 08 May 2012 - 06: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
Posted Image

#3 jcasanova

jcasanova

    Advanced Member

  • Members
  • PipPipPip
  • 61 posts

Posted 08 May 2012 - 06:36 PM

Excellent i'll test it asap

Thanks

#4 duangsin.k

duangsin.k

    Member

  • Members
  • PipPip
  • 10 posts

Posted 25 May 2012 - 08:05 AM

Excellent

#5 stavgian

stavgian

    Member

  • Members
  • PipPip
  • 11 posts

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?



#6 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 20 September 2013 - 07:23 PM

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


____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#7 siarik

siarik

    Member

  • Members
  • PipPip
  • 15 posts

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

 

 



#8 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

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!


____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#9 hurtz

hurtz

    Member

  • Members
  • PipPip
  • 15 posts

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);
    }





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users