连接到 codeigniter 中的另一个数据库并插入

connect to another db in codeigniter and insert

我正在尝试连接到另一个数据库并插入数据,数据在第一个数据库中正确插入但没有插入到第二个数据库中,两个表中的所有字段都相同,下面的代码没有插入 latestdb,下面是我的模型代码,我很确定控制器和视图都很好,如果需要更多详细信息,请告诉我。我正在使用 codeigniter 2。问题是在评论之后 // Insert in pr_users

$this->db->$function($this->myTables['users'], $data);
    $db1['latestdb']['hostname'] = 'localhost';
    $db1['latestdb']['username'] = 'root';
    $db1['latestdb']['password'] = 'passw';
    $db1['latestdb']['database'] = 'latestdb';
    $db1['latestdb']['dbdriver'] = 'mysql';
    $db1['latestdb']['dbprefix'] = '';
    $db1['latestdb']['pconnect'] = TRUE;
    $db1['latestdb']['db_debug'] = TRUE;
    $db1['latestdb']['cache_on'] = FALSE;
    $db1['latestdb']['cachedir'] = '';
    $db1['latestdb']['char_set'] = 'utf8';
    $db1['latestdb']['dbcollat'] = 'utf8_general_ci';
    $db1['latestdb']['swap_pre'] = '';
    $db1['latestdb']['autoinit'] = TRUE;
    $db1['latestdb']['stricton'] = FALSE;
   
   
    $DB2 = $this->load->database($db1, TRUE);
    $DB2->db_select('zipbizzlatestdb');
    $DB2->$function($this->myTables['users'], $data);

    $DB2->insert('pr_users',$data);

我收到错误提示:

遇到错误

您还没有选择要连接的数据库类型。

修改

$DB2 = $this->load->database($db1);

// TRUE parameter tells CI that you'd like to return the database object.
$DB2 = $this->load->database($db1, TRUE);

另请注意

You don’t need to create separate database configurations if you only need to use a different database on the same connection. You can switch to a different database when you need to, like this:

$this->db->db_select('database2_name');

$this->$DB2->your_function( ... )

 ^
  Does not have any such property

$DB2->your_function( .. )

在您的中试试这个 模态

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Test extends CI_Model {

  function __construct()
  {
    parent::__construct();
    $this->another = $this->load->database("anotherdb",true);
  } 

  function get()
  {
    $this->another->select("*");
    $this->another->from("admin");
    $query = $this->another->get();
    return $query->result();
  } 
}
?>