2017-09-19 6 views
1
を挿入します

別のデータベースに接続しようとしていますが、データは最初のdbに正しく挿入されていますが、以下のコードはlatestdbに挿入されていません。私のモデルコードは以下の通りです。コントローラーとビューが問題ないと確信しています。詳細が必要な場合は教えてください。codeigniterの別のDBに接続して

エラーが

を発生しましたあなたが持っている:私は、私はエラーが言って取得していますコメント//がpr_usersに

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

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

を挿入した後、問題があるCodeIgniterの2を使用しています接続するデータベースタイプは選択されていません。

+0

https://stackoverflow.com/questions/8268853/codeigniter-multiple-database-connections](https://stackoverflow.com/questions/8268853/codeigniter-multiple-database-connections [これを確認してください) – siva

答えて

1

修正

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

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

はまた、あなたが唯一の は異なるデータベースを使用する必要がある場合は、別々のデータベース構成を作成する必要はありません

の点に注意してください。同じ接続で

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

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

^ 
    Does not have any such property 

$DB2->your_function(..) 

+0

が動作していない、何かが間違っている、データベース設定がすでにコンフィグレーションを持っています – sunshine

+0

@sunshine、もう1つの問題 '$ DB2-> your_function(..)'は正しいです、 '$ this-> $ DB2-> your_function(。 ..) ' –

+0

@sunshine更新を参照してください –

0

あなた モーダルでこれを試してください:あなたは、このように、異なるデータベースへ スイッチを必要とすることができるとき

<?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(); 
    } 
} 
?>