0

I was trying to connect a multiple database(mysql) in a single codeigniter application? But the database are in different server.

CodeIgniter 3.x

A PHP Error was encountered Severity: Warning

Message: mysqli::real_connect(): (HY000/2002): Connection timed out

Filename: mysqli/mysqli_driver.php

Line Number: 203

Backtrace:

File: /var/www/html/rmp/mautic_sync/mautic_ci/application/controllers/Cron.php Line: 19 Function: database

File: /var/www/html/rmp/mautic_sync/mautic_ci/index.php Line: 315 Function: require_once

1 Answer 1

0

Document: https://www.codeigniter.com/user_guide/database/connecting.html

add new element $db array in application/config/database.php

$db['old'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => 'xxxxx', 'database' => 'you_database_name', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8mb4', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE );

Use:

class YourController extends CI_Controller {

 private $old_db = "";
function __construct() {
    parent::__construct();
    $this->old_db = $this->load->database('old', true);
}
public function index(){
    $query = $this->old_db->get('table_name');
    print_r($query->result());
}

}
Sign up to request clarification or add additional context in comments.

2 Comments

yes it works both db instance are in the same location.. i am getting error only when both are different instances
@MohanPrasanth please provide your code or elaborate about on which places you want to use your db instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.