为了账号安全,请及时绑定邮箱和手机立即绑定

如何将 localhost 数据库连接到另一个服务器数据库并使用 codeigniter

如何将 localhost 数据库连接到另一个服务器数据库并使用 codeigniter

PHP
喵喔喔 2022-10-14 14:39:19
<?php$active_group = 'default';$query_builder = TRUE;$db['default']['hostname'] = 'localhost';$db['default']['username'] = 'campus';$db['default']['password'] = 'Mac@123';$db['default']['database'] = 'campusnew';$db['default']['dbdriver'] = 'mysqli';$db['default']['dbprefix'] = '';$db['default']['pconnect'] = TRUE;$db['default']['db_debug'] = TRUE;$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'] = TRUE;$db['otherdb']['hostname'] = "101.51.150.42";$db['otherdb']['username'] = 'software';$db['otherdb']['password'] = 'Lmsnew';$db['otherdb']['database'] = 'lmsnew';$db['otherdb']['dbdriver'] = "mysqli";$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'] = TRUE;模型函数public function newusersignin(){     $DB2 = $this->load->database('otherdb', TRUE);    $firstName=$this->input->post('firstName');    $lastName=$this->input->post('lastName');    $timezone=$this->input->post('timezone');    $fullname=$firstName.' '.$lastName;    $email=$this->input->post('email');    $phone=$this->input->post('phone');    $ip_address=$this->input->ip_address();    $institutional=$this->input->post('institutional');    $pass=$firstName.mt_rand(1,100);    $username=$email;    $password=$this->hash($pass);}在这个问题中,我只是创建了两个数据库连接,一个用于localhost,另一个用于IP托管在不同的服务器上。现在,当我要在我的IP服务器中插入表单数据时会发生什么,然后它会抛出一个错误,即消息:mysqli::real_connect(): (HY000/2002): 连接被拒绝我不知道为什么会这样?我怎么解决这个问题?请帮我。
查看完整描述

3 回答

?
慕田峪4524236

TA贡献1875条经验 获得超5个赞

您可以检查 URL,而不是将两个数组中的所有内容加倍吗?


只需更改 localhost 以更具体地了解您还需要连接哪个数据库


$is_local = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}";

if (strpos($is_local, 'localhost') !== false) {

    // LOCAL

    $db_name = "db_name";

    $db_user = "root";

    $db_pass = "root_pass";

    $db_host = "localhost";

} else {

    // OTHER

    $db_name = "db_name_2";

    $db_user = "root_live";

    $db_pass = "root_pass_live";

    $db_host = "mysql.live.com";

}

那么你的连接代码从那时起总是一样的


查看完整回答
反对 回复 2022-10-14
?
慕码人2483693

TA贡献1860条经验 获得超9个赞

您上面给出的数据库配置和方法是正确的。您所要做的就是允许远程连接服务器上的数据库。如果您使用的是 CPanel,您可以浏览到“Remote MySQL”并添加访问主机。添加您的 ISP IP 地址以仅允许您的计算机或任何计算机访问“%”。



查看完整回答
反对 回复 2022-10-14
?
慕莱坞森

TA贡献1810条经验 获得超4个赞

数据库配置


CodeIgniter 有一个配置文件,可让您存储数据库连接值(用户名、密码、数据库名称等)。配置文件位于 application/config/database.php。您还可以通过将 database.php 放在相应的环境配置文件夹中来设置特定环境的数据库连接值。


$db['default'] = array(

    'dsn'   => '',

    'hostname' => 'localhost',

    'username' => 'root',

    'password' => '',

    'database' => 'database_name',

    'dbdriver' => 'mysqli',

    'dbprefix' => '',

    'pconnect' => TRUE,

    'db_debug' => TRUE,

    'cache_on' => FALSE,

    'cachedir' => '',

    'char_set' => 'utf8',

    'dbcollat' => 'utf8_general_ci',

    'swap_pre' => '',

    'encrypt' => FALSE,

    'compress' => FALSE,

    'stricton' => FALSE,

    'failover' => array()

);

插入数据


$data = array(

    'title' => 'My title',

    'name' => 'My Name',

    'date' => 'My date'

);


$this->db->insert('mytable', $data);


查看完整回答
反对 回复 2022-10-14
  • 3 回答
  • 0 关注
  • 136 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号