3
In my.php database I created two connections:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'sitejc';
$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']['date_default_timezone_set'] = 'America/Sao_Paulo';
$db['parceiro']['hostname'] = 'localhost';
$db['parceiro']['username'] = 'root';
$db['parceiro']['password'] = '';
$db['parceiro']['database'] = 'sistema2';
$db['parceiro']['dbdriver'] = 'mysql';
$db['parceiro']['dbprefix'] = '';
$db['parceiro']['pconnect'] = TRUE;
$db['parceiro']['db_debug'] = TRUE;
$db['parceiro']['cache_on'] = FALSE;
$db['parceiro']['cachedir'] = '';
$db['parceiro']['char_set'] = 'utf8';
$db['parceiro']['dbcollat'] = 'utf8_general_ci';
$db['parceiro']['swap_pre'] = '';
$db['parceiro']['autoinit'] = TRUE;
$db['parceiro']['stricton'] = FALSE;
$db['parceiro']['date_default_timezone_set'] = 'America/Sao_Paulo';
/* End of file database.php */
/* Location: ./application/config/database.php */
Inside my controller, I load this way:
$parceiro_db = $this->load->database('parceiro', TRUE);
However, when making a request from the selected database returns me error.
Form of requisition:
class Cadastro extends MY_Controller {
public function __construct() {
parent::__construct();
$this->load->model('model_cadastro');
$parceiro = $this->load->database('parceiro', TRUE);
}
# Cadastro para Empresa
public function empresa($metodo=NULL) {
if($this->input->post('submit')){
$clientes = $parceiro->get('cliente')->result();
}
Error:
Fatal error: Call to a Member Function get() on a non-object in /home/jcentreg/public_html/sitejc/site/modules/cadastro/controllers/cadastro.php on line 21
How do I make this multi connection?
If I call ->db-> it looks for the current db, I have another db on the connection, so I called it partner
– Sr. André Baill
@Andrébaill, then I remade the issue.! looks like it would be!
– novic
Load: $partner = $this->load->database('partner', TRUE); - I called it like this: $customers = $partner->get('customers')->result();, Fatal error return: Call to a Member Function get() on a non-object in D: Sites Localhost Easyphp-Devserver-14.1VC11 data localweb jcdeliversrapidas site modules cadastro controllers cadastro.php on line 18
– Sr. André Baill
@Andrébaill in your database.php file, complete post on your question! I think you’re wrong, please edit your question and put it complete!
– novic
I edited it, take a look
– Sr. André Baill
@Andrébaill has a configuration error, or else version of
PHP
that is not configured, so put what I said in the answer and test! use alwaysmysqli
forPHP
– novic
So I did $this->db->get('x')->result(), and it comes, but in the documentation it says I have to call from the variable I put in connection...
– Sr. André Baill
Let’s go continue this discussion in chat.
– novic
@Andrébaill enters the chat, because, you are all confused, what I did is correct... look
$db
is a variable that receives connection settings.– novic