0

我们在 yii 框架中使用 config.php 文件中定义的 mysql db 连接来做应用程序。然而由于某种原因,我们想连接到其他服务器 mysql db。我可以这样做吗(不使用现有连接):

public function init_db() 
{ 
    $db_handler = mysqli_connect('http://xxx.xxx.xxx.xxx', 'name', 'password', 'db_name');
    $db_handler->set_charset('utf8');

    // check connection 
    if (mysqli_connect_errno()) {
        throw new Exception ("Error connecting to DB : " . mysqli_connect_error()  );
    }              

    // set autocommit to off 
    mysqli_autocommit($db_handler, FALSE);

    mysqli_query ($db_handler, "set time_zone='Europe/Minsk'");

    return $db_handler;
}

当我尝试时,我得到了错误: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: hostname nor servname provided, or not known

在这条线上$db_handler = mysqli_connect('http://xxx.xxx.xxx.xxx', 'name', 'password', 'db_name');

是否有任何解决方法,破解短时间连接以从外部服务器(不是本地主机)获取一些数据?

更新

我刚碰到这个踏板。然而,当我尝试:

$connection=new CDbConnection($dsn,$username,$password);
$connection->active=true;

yii 发出 CDbException:

CDbConnection failed to open the DB connection: could not find driver

4

1 回答 1

1

在您的配置文件中,只需添加另一个连接设置:

    'db' => array(
        'connectionString' => 'mysql:host=127.0.0.1;dbname=test',
        ....
    ),
    'otherDb' => array(
        'connectionString' => 'mysql:host=127.0.0.1;dbname=test2',
        ....
    ),

并像 Yii::app()->otherDb 一样使用它

于 2014-04-11T13:03:38.633 回答