0
votes

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

1 Answers

0
votes

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());
}

}