简体   繁体   中英

How to configure two database in codeigniter.?

I am setting my two database like this

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'sql.domain.com',
    'username' => 'u_name',
    'password' => 'pass',
    'database' => 'DB_1',
    '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
);

$db['User_DB'] = array(
    'dsn'   => '',
    'hostname' => 'sql.domain.com',
    'username' => 'u_name',
    'password' => 'pass',
    'database' => 'DB_2',
    '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
);

I am accessing above databases in controller using below code.

   public function index(){

            $db_anal = $this->load->database('User_DB', TRUE);              
            $p_name = $this->input->post('p_name');


            $user = $this->session->userdata('E_Id');

            $tabs_data['res1'] = $this->db->distinct()->select('p_type')->from('tab_1')->get()->result();       


            //$paper_name = $data;

            $UP_array = array(
                'Status' => 'success',
                'User_id' => $user,                 
            );
            $query1['res1'] = $this->db->select('*')->from('tab_1')->where('p_type',$p_name)->get();        
            $query1['res2'] = $this->db->select('*')->from('tab_2')->where($UP_array)->get();

            $query1['res3'] = $db_anal->select('*')->from('tab_3')->where('User_id',$user)->get();


            echo json_encode($query1);
            return true;                                

}

I am getting null value by using above code any one can tell me. Is it problem with second database.

How can I solve this any one can help me.?

I am getting below null in console also am using ajax to get data from codeigniter.

{"res1":{"conn_id":{"affected_rows":null,"client_info":null,"client_version":null,"connect_errno":null,"connect_error":null,"errno":null,"error":null,"error_list":null,"field_count":null,"host_info":null,"info":null,"insert_id":null,"server_info":null,"server_version":null,"stat":null,"sqlstate":null,"protocol_version":null,"thread_id":null,"warning_count":null},"result_id":{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null},"result_array":[],"result_object":[],"custom_result_object":[],"current_row":0,"num_rows":null,"row_data":null},"res2":{"conn_id":{"affected_rows":null,"client_info":null,"client_version":null,"connect_errno":null,"connect_error":null,"errno":null,"error":null,"error_list":null,"field_count":null,"host_info":null,"info":null,"insert_id":null,"server_info":null,"server_version":null,"stat":null,"sqlstate":null,"protocol_version":null,"thread_id":null,"warning_count":null},"result_id":{"current_field":null,"field_count":null,"lengths" :null,"num_rows":null,"type":null},"result_array":[],"result_object":[],"custom_result_object":[],"current_row":0,"num_rows":null,"row_data":null},"res3":{"conn_id":{"affected_rows":null,"client_info":null,"client_version":null,"connect_errno":null,"connect_error":null,"errno":null,"error":null,"error_list":null,"field_count":null,"host_info":null,"info":null,"insert_id":null,"server_info":null,"server_version":null,"stat":null,"sqlstate":null,"protocol_version":null,"thread_id":null,"warning_count":null},"result_id":{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null},"result_array":[],"result_object":[],"custom_result_object":[],"current_row":0,"num_rows":null,"row_data":null}} teq:863:6

getting everything null any one can help me.?

the problem is the CI Code

According to the docs , get returns a (query-)result object

If you want the data out of it try the following

$objQuery = $db_anal->select('*')->from('tab_3')->where('User_id',$user)->get();
if ($objQuery->num_rows() > 0)
{
    $query1['res3'] = $objQuery->result();
}

According to the latest CI Docs for using multiple database connection you should not call DB with $this->db->query() but with returned DB object for each connection you make like in your exmaple:

$db_anal = $this->load->database('User_DB', TRUE);

This TRUE flag is indicating to the CI to return DB object and you are storing it in $db_anal so now you sholud use this DB connection as $db_anal->query()...

Change in your code all calls like this:

 $tabs_data['res1'] = $this->db->distinct()->select('p_type')->from('tab_1')->get()->result(); 

to this:

 $tabs_data['res1'] = $db_anal->distinct()->select('p_type')->from('tab_1')->get()->result(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM