Welcome Guest, Not a member yet? Register   Sign In
MySQL Database (Multi Database)
#1

[eluser]Unknown[/eluser]
Hi,
Can any one help in connecting to multi databases??? this my config/database.php file:

Code:
$active_group = "MainDB";

$db['MainDB']['hostname'] = "localhost";
$db['MainDB']['username'] = "Shahabv";
$db['MainDB']['password'] = "123456";
$db['MainDB']['database'] = "Main";
$db['MainDB']['dbdriver'] = "mysql";
$db['MainDB']['dbprefix'] = "";
$db['MainDB']['active_r'] = TRUE;
$db['MainDB']['pconnect'] = TRUE;
$db['MainDB']['db_debug'] = TRUE;
$db['MainDB']['cache_on'] = FALSE;
$db['MainDB']['cachedir'] = "system/cache/";

$db['NewsDB']['hostname'] = "localhost";
$db['NewsDB']['username'] = "Shahabv";
$db['NewsDB']['password'] = "123456";
$db['NewsDB']['database'] = "News";
$db['NewsDB']['dbdriver'] = "mysql";
$db['NewsDB']['dbprefix'] = "";
$db['NewsDB']['active_r'] = TRUE;
$db['NewsDB']['pconnect'] = TRUE;
$db['NewsDB']['db_debug'] = TRUE;
$db['NewsDB']['cache_on'] = FALSE;
$db['NewsDB']['cachedir'] = "system/cache/";

and i use this code in my controller :

Code:
$NewsDB->$this->load->database('NewsDB', TRUE);
        
$sql = "SELECT Title, sKey
        From Site_News";
        
$Query = $NewsDB->query($sql);
        
if($NewsDB->num_rows() > 0) {

       foreach ($Query->result_array() as $row)
       {
           echo $row["Title"];
           echo $row["sKey"];
       }
            
}

Is it Truee??? Thanks Alot
#2

[eluser]MadZad[/eluser]
Shahab,
I believe the first line of your controller should be:
Code:
$NewsDB = $this->load->database('NewsDB', TRUE);

Also, if num_rows call not working, try $Query->num_rows()

You can simplify your db config by eliminating all the NewsDB lines and use:
Code:
$db['NewsDB'] = $db['MainDB'];
$db['NewsDB']['database'] = "News";
#3

[eluser]Cahyono[/eluser]
I have code like this :
Code:
$fname2 = 'mailing.sql';
$this->load->database('default1', TRUE);
$this->load->dbutil();
$this->dbutil->optimize_database();

$prefs = array(
                'format'      => 'txt',             // gzip, zip, txt
                'filename'    => $fname2,    // File name - NEEDED ONLY WITH ZIP FILES
                'add_drop'    => TRUE,              // Whether to add DROP TABLE statements to backup file
                'add_insert'  => TRUE,              // Whether to add INSERT data to backup file
                'newline'     => "\n"               // Newline character used in backup file
              );
$backup = $this->dbutil->backup($prefs);
write_file($fname2, $backup);

but when backup why it still using config 'default' as active database and not 'default1' when write sql file?




Theme © iAndrew 2016 - Forum software by © MyBB