尽管存在现有的yii框架DB连接,我可以建立替代数据库连接
-
21-12-2019 - |
题
我们在使用config.php文件中定义的mysql db连接工作的yii框架中的应用程序。 然而,由于某种原因我们希望连接到其他服务器MySQL数据库。我可以这样做(不使用存在的连接):
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');
是否有任何解决方法,黑客可以获得短时间连接,从外部服务器获取一些数据(不是localhost)?
更新
我刚刚遇到这个 tread 。 然而,当我尝试时:
$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() - >其他方式
不隶属于 StackOverflow