使用LIVE数据库连接到LAN(XAMPP)数据库连接,在Codeigniter中连接多个数据库?

时间:2017-02-15 02:59:08

标签: mysql database codeigniter networking remote-access

是否可以将互联网上托管的PHP服务器连接到家庭局域网上的数据库?

我该怎么办?这是我的示例连接数据库:

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'www.mydomainname.com';
$db['default']['username'] = 'myusername';
$db['default']['password'] = 'mypassword';
$db['default']['database'] = 'mydabatasenameoncloud';
$db['default']['dbdriver'] = 'mysqli';  
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

//-- Poll.php (My LAN Connection.)

$config['hostname'] = 'localhost';
$config['username'] = 'root';
$config['password'] = ''; 
$config['database'] = 'mydatabasenameonlan';
$config['dbdriver'] = 'mysqli';
$config['dbprefix'] = '';
$config['pconnect'] = FALSE;
$config['db_debug'] = FALSE;
$config['cache_on'] = FALSE;
$config['cachedir'] = '';
$config['char_set'] = 'utf8';
$config['dbcollat'] = 'utf8_general_ci';
$config['swap_pre'] = '';
$config['autoinit'] = TRUE;
$config['stricton'] = FALSE;

$DB2 = $this->load->database($config, TRUE);
$DB3 = $this->load->database('default', TRUE);

$check_if_connected = $DB2->initialize();

所以,这是我的Reversed数据库连接。云到Lan数据库连接。但是我收到了一个错误。我可以在XAMPP上远程MySQL吗?

1 个答案:

答案 0 :(得分:0)

您正确进行默认连接,如下所示 -

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "mydabatasenameonlan";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
$db['default']['swap_pre'] = "";
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

但是在你的第二个数据库连接中定义了如下的配置 -

$db['otherdb']['hostname'] = "www.mydomainname.com";
$db['otherdb']['username'] = "myusername";
$db['otherdb']['password'] = "mypassword";
$db['otherdb']['database'] = "mydatabasenameoncloud";
$db['otherdb']['dbdriver'] = "mysql";
$db['otherdb']['dbprefix'] = "";
$db['otherdb']['pconnect'] = TRUE;
$db['otherdb']['db_debug'] = FALSE;
$db['otherdb']['cache_on'] = FALSE;
$db['otherdb']['cachedir'] = "";
$db['otherdb']['char_set'] = "utf8";
$db['otherdb']['dbcollat'] = "utf8_general_ci";
$db['otherdb']['swap_pre'] = "";
$db['otherdb']['autoinit'] = TRUE;
$db['otherdb']['stricton'] = FALSE;

然后您可以连接到云数据库,如下所示 -

$otherdb = $this->load->database('otherdb', TRUE);

你也可以看到这个答案 - Codeigniter - multiple database connections