提问人:Avi 提问时间:3/3/2020 更新时间:3/3/2020 访问量:572
如何使用codeigniter将localhost数据库连接到另一个服务器数据库并插入表单值?[复制]
How to connect localhost database to another server database and insert form value using codeigniter? [duplicate]
问:
<?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);
$insertnewuser= array('name' =>$fullname,'email'=>$email,'phone'=>$phone,'username'=>$username,'password'=>$password,'usertype'=>'ClgAdmin','create_date'=>date('y-m-d H:i:s'),'create_userID'=>1,'create_username'=>'campus','systemadminactive'=>0,'timezone'=>$timezone,'status'=>1 );
$this->db->insert('admin',$insertnewuser);
$DB2->insert('admin',$insertnewuser);
}
在这个问题中,我只是创建两个数据库连接,一个用于,另一个托管在不同的服务器上。现在,当我要在我的服务器中插入表单数据时会发生什么,然后它会抛出一个错误,即localhost
IP
IP
消息:mysqli::real_connect():(HY000/2002):连接被拒绝
我不知道为什么会这样?我该如何解决这个问题?请帮帮我。
谢谢
答:
0赞
Schreyers
3/3/2020
#1
与其将两个数组中的所有内容加倍,不如检查 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";
}
然后,从那时起,您的连接代码始终相同
评论
0赞
ADyson
3/3/2020
我真的希望您不要经常使用Web应用程序的“root”帐户来访问数据库,正如此示例所暗示的那样......
0赞
Rahul Kr Daman
3/3/2020
#2
数据库配置
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);
评论
0赞
Nico Haase
3/3/2020
你改变了什么,为什么?请通过编辑来为您的答案添加一些解释,以便其他人可以从中学习
0赞
Asif Aziz
3/3/2020
#3
上面给出的数据库配置和方法是正确的。您所要做的就是允许远程连接服务器上的数据库。如果您使用的是 CPanel,您可以浏览到“远程 MySQL”并添加访问主机。添加您的 ISP IP 地址以仅允许您的计算机或“%”供任何计算机访问。
评论
'localhost';
'127.0.0.1';