2011-11-25 19 views
34

Devo recuperare un database MySQL dal database master e quindi connettersi a quel database e recuperare alcuni record.Codeigniter - più connessioni al database

Voglio dire che tenendo un database voglio caricare un altro database.

È possibile con Codeigniter? In questo momento sto usando le seguenti linee di codice nel mio modello.

function connectDb($credential) 
{ 

    $config['hostname'] = $credential['server']; 
    $config['username'] = $credential['username']; 
    $config['password'] = $credential['password']; 
    $config['database'] = $credential['database']; 
    $config['dbdriver'] = "mysql"; 
    $config['dbprefix'] = ""; 
    $config['pconnect'] = FALSE; 
    $config['db_debug'] = TRUE; 
    $config['cache_on'] = FALSE; 
    $config['cachedir'] = ""; 
    $config['char_set'] = "utf8"; 
    $config['dbcollat'] = "utf8_general_ci"; 

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

    $DB2->db->select('first_name,last_name'); 
    $query = $DB2->db->get('person'); 
    print_r($query); 

} 

non funziona in altro modo?

+0

Definisci "non funzionante" –

risposta

60

Si dovrebbe fornire il secondo informazioni del database in `application// database.php'

Normalmente, si dovrebbe impostare il gruppo default database di configurazione, in questo modo:

$db['default']['hostname'] = "localhost"; 
$db['default']['username'] = "root"; 
$db['default']['password'] = ""; 
$db['default']['database'] = "database_name"; 
$db['default']['dbdriver'] = "mysql"; 
$db['default']['dbprefix'] = ""; 
$db['default']['pconnect'] = TRUE; 
$db['default']['db_debug'] = FALSE; 
$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'] = FALSE; 

Si noti che le informazioni di login e le impostazioni sono fornite nell'array $db['default'].

È quindi possibile aggiungere un altro database in un nuovo array: chiamiamolo 'otherdb'.

$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; 

Ora, utilizzare effettivamente il secondo database, è necessario inviare il collegamento a un altro variabel che è possibile utilizzare nel modello:

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

Che dovrebbe farlo. La documentazione per il collegamento a più database può essere trovato qui: http://codeigniter.com/user_guide/database/connecting.html

6

Guardando il codice, l'unica cosa che vedo male, è quando si tenta di caricare il secondo database:

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

Quando si vuoi recuperare l'oggetto del database, devi passare TRUE nel secondo argomento.

Dal Codeigniter User Guide:

Impostando il secondo parametro a TRUE (booleano) la funzione verrà restituire l'oggetto di database.

Quindi, il codice dovrebbe essere invece:

$DB2=$this->load->database($config, TRUE); 

che renderà il lavoro.

+1

ho provato lo stesso ma non ha funzionato :( –

8

Utilizzare questo.

$dsn1 = 'mysql://user:[email protected]/db1'; 
$this->db1 = $this->load->database($dsn1, true);  

$dsn2 = 'mysql://user:[email protected]/db2'; 
$this->db2= $this->load->database($dsn2, true);  

$dsn3 = 'mysql://user:[email protected]/db3'; 
$this->db3= $this->load->database($dsn3, true); 

Uso

$this->db1 ->insert('tablename', $insert_array); 
$this->db2->insert('tablename', $insert_array); 
$this->db3->insert('tablename', $insert_array); 
+0

ciao signore, potresti spiegare dove abbiamo dato nome utente, password, nome del database con un semplice esempio, possiamo dare questa connessione nel modello di contrazione. – whoami

+0

utente = nome utente, password = password, db1 = nome database, localhost = nome host – Rayiez

9

Il modo migliore è quello di utilizzare diversi gruppi di database. Se si desidera continuare a utilizzare il database master come di consueto ($ this-> db), basta disattivare l'opzione di configurazione persistente con collegamento ai database secondari.Solo database master dovrebbe funzionare con connessione persistente:

master database

$db['default']['hostname'] = "localhost"; 
$db['default']['username'] = "root"; 
$db['default']['password'] = ""; 
$db['default']['database'] = "database_name"; 
$db['default']['dbdriver'] = "mysql"; 
$db['default']['dbprefix'] = ""; 
$db['default']['pconnect'] = TRUE; 
$db['default']['db_debug'] = FALSE; 
$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'] = FALSE; 

database secondario (avviso pconnect è impostata su false)

$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'] = FALSE; 
$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; 

Quindi è possibile utilizzare i database secondari come oggetti banca dati durante l'utilizzo del database master come al solito:

// use master dataabse 
$users = $this->db->get('users'); 

// connect to secondary database 
$otherdb = $this->load->database('otherdb', TRUE); 
$stuff = $otherdb->get('struff'); 
$otherdb->insert_batch('users', $users->result_array()); 

// keep using master database as usual, for example insert stuff from other database 
$this->db->insert_batch('stuff', $stuff->result_array()); 
+0

Ho scritto un articolo su Crea più connessioni di database nelle applicazioni CodeIgniter. Si prega di dare un'occhiata e dare i vostri suggerimenti https://www.cloudways.com/blog/connect-multiple-databases-codeigniter/ –

Problemi correlati