기존 Yii-Framework DB 연결에도 불구하고 대체 DB 연결을 설정할 수 있습니까?

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

문제

CONFIG.PHP 파일에 정의 된 MySQL DB 연결을 사용하여 Yii Framework에서 응용 프로그램을 수행합니다. 그러나 어떤 이유로 다른 서버 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