• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DB » Multiple connections CI3 ?

#1
Hi guys.

You can easily set up a connection to my database by database.php file. The problem is that when I try to configure more than one connection my application crashes. How do I configure multiple connections in database.php power and give a CRUD in Model.php?

Reply

#2
Connecting to Multiple Databases: https://codeigniter.com/user_guide/datab...cting.html

If you're autoloading you should specify the default and load the second one manually.
Reply

#3
(04-14-2016, 04:11 PM)spjonez Wrote: Connecting to Multiple Databases: https://codeigniter.com/user_guide/datab...cting.html

If you're autoloading you should specify the default and load the second one manually.

Can you give an example ?

Reply

#4
$DB1 = $this->load->database('group_one', TRUE);
$DB2 = $this->load->database('group_two', TRUE);
Reply

#5
(04-18-2016, 12:49 PM)dmyers Wrote: $DB1 = $this->load->database('group_one', TRUE);
$DB2 = $this->load->database('group_two', TRUE);

I will detail my nonfunctional code?

» File Setup » database.php

$active_group = 'dbone';
$query_builder = TRUE;

$db['dbone'] = array(
'dsn' => '',
'hostname' => '000.000....',
'username' => '',
'password' => '',
'database' => '',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

//$active_group = 'dbtwo';
//$query_builder = TRUE;

$db['dbtwo'] = array(
'dsn' => '',
'hostname' => '000.000...',
'username' => '',
'password' => '',
'database' => '',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

File_model.php


<?php defined('BASEPATH') OR exit('No direct script access allowed');

class File_model extends CI_Model {

public function insert() {
$dbs = $this->load->database('screens', TRUE);
$email = 'myemail';
$this->db->insert('ws_user_on', $$email);
}
}

I can not see error? And simply does not work?

Reply

#6
(04-19-2016, 05:42 PM)jocm.aguiar Wrote:
(04-18-2016, 12:49 PM)dmyers Wrote: $DB1 = $this->load->database('group_one', TRUE);
$DB2 = $this->load->database('group_two', TRUE);

I will detail my nonfunctional code?

» File Setup » database.php

$active_group = 'dbone';
$query_builder = TRUE;

$db['dbone'] = array(
'dsn' => '',
'hostname' => '000.000....',
'username' => '',
'password' => '',
'database' => '',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

//$active_group = 'dbtwo';
//$query_builder = TRUE;

$db['dbtwo'] = array(
'dsn' => '',
'hostname' => '000.000...',
'username' => '',
'password' => '',
'database' => '',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

File_model.php


<?php defined('BASEPATH') OR exit('No direct script access allowed');

class File_model extends CI_Model {
 
   public function insert() {
      $dbs = $this->load->database('screens', TRUE);
      $email = 'myemail';
      $this->db->insert('ws_user_on', $$email);
   }
}

I can not see error? And simply does not work?

1st: You selected a database config named 'screens'? but your database configs are dbone and dbtwo.
2nd: If 'screens' did exist you assigned it to a object $dbs BUT used the $this->db object? that would be $dbs->insert('ws_user_on', $$email);
3rd: You tried to use a variable named myemail because you used a variable variable and myemail doesn't contain anything because it's not declared.

DMyers
Reply

#7
I made the settings as and did not work ?

Reply

#8
$this->one = $this->load->database('dbone', true);
$this->two = $this->load->database('dbtwo', true);

$this->one->select('*')->from('some_table')->get();

$this->two->query('select * from some_table');
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.