既存の Yii-framework データベース接続にもかかわらず、代替のデータベース接続を確立できますか?

StackOverflow https://stackoverflow.com//questions/23013138

質問

config.php ファイルで定義された mysql db 接続を使用して、yii フレームワークでアプリケーションを実行します。それでも、何らかの理由で、他のサーバーの 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

役に立ちましたか?

解決

構成ファイルに別の接続設定を追加するだけです。

    '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のように使用します

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top