Multi connections Mysql Codeigniter

Asked

Viewed 536 times

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?

1 answer

2


Would not be $parceiro_db?

$clientes = $parceiro_db->get('clientes')->result();

Doc - Connecting to Multiple Databases

There is a problem with the setup on $db['parceiro']['dbdriver'] is set as mysql put in place mysqli:

$db['parceiro']['hostname'] = 'localhost'; 
$db['parceiro']['username'] = 'root';
$db['parceiro']['password'] = '';
$db['parceiro']['database'] = 'sistema2';
$db['parceiro']['dbdriver'] = 'mysqli';
$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';

$db = $this->load->database("parceiro", TRUE);
var_dump(  $db->get("cliente")->result_array() );

Coding error:

class Cadastro extends MY_Controller 
{
    private $parceiro;
    public function __construct() 
    {
        parent::__construct();
        $this->load->model('model_cadastro');
        $this->parceiro = $this->load->database('parceiro', TRUE);
    }

    # Cadastro para Empresa
    public function empresa($metodo=NULL) 
    {
        if($this->input->post('submit'))
        {
            $clientes = $this->parceiro->get('cliente')->result();
        }

How a variable was used to work in the methods has to use $this to assign and use.

Examples: Soen - Codeigniter: Multiple Databases - Accessing database config in a Second database

  • If I call ->db-> it looks for the current db, I have another db on the connection, so I called it partner

  • @Andrébaill, then I remade the issue.! looks like it would be!

  • 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

  • @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!

  • I edited it, take a look

  • @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 always mysqli for PHP

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

  • @Andrébaill enters the chat, because, you are all confused, what I did is correct... look $db is a variable that receives connection settings.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.