douhuan1950 2019-06-17 09:56
浏览 92

在不同实例中的多个数据库连接以连接codeigniter应用程序

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条回答 默认 最新

  • douyan4470 2019-06-17 12:18
    关注

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

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算