【问题标题】:codeigniter insert data into two databases using localhost?codeigniter 使用 localhost 将数据插入两个数据库?
【发布时间】:2017-02-11 19:18:57
【问题描述】:

我正在处理安装在 localhost 和 Web 服务器上的 codeigniter 应用程序和应用程序,但实际上我希望数据应该使用 localhost 插入到 localhost 数据库和 Web 服务器数据库中?

如何在 database.php 文件中连接网络服务器数据库以及如何在 CI_Controller 函数中使用网络服务器连接?

【问题讨论】:

    标签: php database codeigniter localhost


    【解决方案1】:

    你可以看这个帖子Codeigniter - multiple database connections 但基本上是这样的 将此添加到 database.php

    $db['otherdb']['hostname'] = "localhost";
    $db['otherdb']['username'] = "root";
    $db['otherdb']['password'] = "";
    $db['otherdb']['database'] = "other_database_name";
    $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;
    

    你可以像这样从模型中调用它

    function my_model_method()
    {
      $otherdb = $this->load->database('otherdb', TRUE); // the TRUE paramater tells CI that you'd like to return the database object.
    
      $query = $otherdb->select('first_name, last_name')->get('person');
      var_dump($query);
    }
    

    【讨论】:

    • 请问如何一次插入两个数据库?
    • 我无法理解您给我发送插入示例?
    【解决方案2】:

    你可以这样做

    function my_model_method()
    {
    $otherdb = $this->load->database('otherdb', TRUE);
    $data= array('name'=>'John');
    $database1=$this->db->insert('students', $data); //default db
    $database2 = $otherdb->insert('students',$data); //the other db
    }
    

    【讨论】:

    • 数据插入实时数据库而不是本地数据库?
    • 我不知道你所说的“实时数据库”是什么意思,你可以在任何你想要的地方配置数据库服务器,可以是本地主机或来自亚马逊`$db['db']['hostname' ] = "本地主机";或 $db['otherdb']['hostname'] = "mydbinstance.abcdefghijkl.us-east-1.rds.amazonaws.com";`
    猜你喜欢
    • 2013-04-04
    • 2016-09-18
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    相关资源
    最近更新 更多