CodeIgniter Forums
Can some one help me with this database connectiong issue - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Can some one help me with this database connectiong issue (/thread-59156.html)



Can some one help me with this database connectiong issue - El Forum - 08-30-2013

[eluser]tiffany1988[/eluser]
HI Guys
Hey Guys , Im fairly new to this CI framework , i was just trying to write a code to connect to multiple database where i am really getting sick of this method . if some one has already worked on this see if you guys can help . Thank you Smile Smile



Can some one help me with this database connectiong issue - El Forum - 08-30-2013

[eluser]PravinS[/eluser]
Check this

http://ellislab.com/codeigniter/user-guide/database/configuration.html


Can some one help me with this database connectiong issue - El Forum - 08-30-2013

[eluser]tiffany1988[/eluser]
[quote author="PravinS" date="1377930148"]Check this

http://ellislab.com/codeigniter/user-guide/database/configuration.html[/quote]

Hey Pravin ,

Thank you . I did check that page but it doesn't seem to help either.

Here's what we are trying to do .

we have multiple localhost servers with different IP's we are tryig to fetch the tables of that servers by selecting the server name .

First you will select the server name :
We fetch the server credentials using that name
then we have to pass that to the database query function

this is the code we are working with see if you can help me .

thank you so much honey.

Code:
<?php

class M_main extends CI_Model
{
      //  var $db_group_name = "default";
        function __construct(){

        parent::__construct();
        $this->load->database();
      //  $this->{$this->db_group_name} = $this->load->database($this->db_group_name, TRUE);
  
        }
        
             function get_serv_list()
        {
            //echo "test get_server_list";
               $this->db->select('srv_name');
               $query = $this->db->get("srv_mng");
               $result= $query->result_array();
               return $result;
            
        }
        
            function get_db_list($ser_name)
            {
               // echo $ser_name;
                $srv_cred=$this->get_ser_cred($ser_name);
               // print_r($srv_cred);
                $db_list=$this->get_db_lst($srv_cred);
            }
            
            function get_ser_cred($ser_name)
            {
               $this->db->where('srv_name',$ser_name);
               $query = $this->db->get('srv_mng');
               return ($query->result_array());
            }

// here passing the serv credentials
            function get_db_lst($srv_cred)
            {
                //print_r($srv_cred);
                    foreach ($srv_cred as $key => $value) {
                  //  print_r($value);
                    $ip_add = $value['ip_add'];
                    $uname=$value['sql_uname'];
                    $pswd =$value['pswd'];
                    }
                    $this->connectDb($ip_add,$uname,$pswd);
            }
        

// trying to connect to that database    
            function connectDb($ip_add,$uname,$pswd)
            {
                
//                $db['otherdb']['hostname'] = $ip_add;
//                $db['otherdb']['username'] = $uname;
//                $db['otherdb']['password'] = $pswd;
//                $db['otherdb']['database'] = "";
//                $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;
                
                $db['alternate']['hostname'] = $ip_add;
                $db['alternate']['username'] = $uname;
                $db['alternate']['password'] = $pswd;
                $db['alternate']['database'] = "CARDZME";
                $db['alternate']['dbdriver'] = "mysql";
                $db['alternate']['dbprefix'] = "";
                $db['alternate']['pconnect'] = TRUE;
                $db['alternate']['db_debug'] = TRUE;
                $db['alternate']['cache_on'] = FALSE;
                $db['alternate']['cachedir'] = "";
                $db['alternate']['char_set'] = "utf8";
                $db['alternate']['dbcollat'] = "utf8_general_ci";
                
                
              
               // echo"<br>.between.<br>";
               $otherdb =  $this->load->database($db['alternate'], TRUE);
                print_r($otherdb);
                $dbsel = $this->db->select('alternate');
                
               // print_r($dbsel);
             //  $this->legacy_db->dbutil();
            //   $query = $this->dbutil->list_databases();
//               print_r($query);
//               var_dump($query);
              // $dbutil  =  $otherdb->dbutil();
              //
              // print_r($dbutil);
              // $query = $otherdb->select('first_name, last_name')->get('person');
              //  $query = $this->dbutil->list_databases();
              // print_r($query);
                exit();
            }
}          

    ?&gt;



Can some one help me with this database connectiong issue - El Forum - 08-31-2013

[eluser]PravinS[/eluser]
you can save the group name in database, so you last two function get_db_lst and connectDb will be like this

Code:
function get_db_lst($srv_cred)
{
  foreach ($srv_cred as $key => $value)
  {
   $ip_add = $value['ip_add'];
   $uname=$value['sql_uname'];
   $pswd =$value['pswd'];
   $group = $value['pswd'];
  }
  $this->connectDb($group,$ip_add,$uname,$pswd);
}
        

// trying to connect to that database    
            function connectDb($group,$ip_add,$uname,$pswd)
            {
                $db[$group]['hostname'] = $ip_add;
                $db[$group]['username'] = $uname;
                $db[$group]['password'] = $pswd;
                $db[$group]['database'] = "CARDZME";
                $db[$group]['dbdriver'] = "mysql";
                $db[$group]['dbprefix'] = "";
                $db[$group]['pconnect'] = TRUE;
                $db[$group]['db_debug'] = TRUE;
                $db[$group]['cache_on'] = FALSE;
                $db[$group]['cachedir'] = "";
                $db[$group]['char_set'] = "utf8";
                $db[$group]['dbcollat'] = "utf8_general_ci";
               $this->otherdb =  $this->load->database($group, TRUE);
               print_r($this->otherdb);
              
            }



Can some one help me with this database connectiong issue - El Forum - 08-31-2013

[eluser]tiffany1988[/eluser]
[quote author="PravinS" date="1377934736"]you can save the group name in database, so you last two function get_db_lst and connectDb will be like this

Code:
function get_db_lst($srv_cred)
{
  foreach ($srv_cred as $key => $value)
  {
   $ip_add = $value['ip_add'];
   $uname=$value['sql_uname'];
   $pswd =$value['pswd'];
   $group = $value['pswd'];
  }
  $this->connectDb($group,$ip_add,$uname,$pswd);
}
        

// trying to connect to that database    
            function connectDb($group,$ip_add,$uname,$pswd)
            {
                $db[$group]['hostname'] = $ip_add;
                $db[$group]['username'] = $uname;
                $db[$group]['password'] = $pswd;
                $db[$group]['database'] = "CARDZME";
                $db[$group]['dbdriver'] = "mysql";
                $db[$group]['dbprefix'] = "";
                $db[$group]['pconnect'] = TRUE;
                $db[$group]['db_debug'] = TRUE;
                $db[$group]['cache_on'] = FALSE;
                $db[$group]['cachedir'] = "";
                $db[$group]['char_set'] = "utf8";
                $db[$group]['dbcollat'] = "utf8_general_ci";
               $this->otherdb =  $this->load->database($group, TRUE);
               print_r($this->otherdb);
              
            }
[/quote]

$group = $value['pswd'];

why are you storing password in that $group ??

i believe its by mistake...O.o


Can some one help me with this database connectiong issue - El Forum - 08-31-2013

[eluser]PravinS[/eluser]
sorry its not password, its group name, you need to store group name in database, then u can use it

$group = $value[‘group_name’];




Can some one help me with this database connectiong issue - El Forum - 09-02-2013

[eluser]tiffany1988[/eluser]
Finally Found the solution !! hope this helps some more people!!!

This code here is for :

1.Selecting Different databases from different localhost servers DYNAMICALLY!!!!


2. Note that the primary database.php file has to be configured with a database initially otherwise this won't work



Thanks Guys!!

Code:
&lt;?php

//This is the database.php ----------------------------------------------


$active_group = 'default';
$active_record = TRUE;


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


// This is the model !!!-----------------------------------------------------------


class M_main extends CI_Model
{
      //  var $db_group_name = "default";
        function __construct(){

        parent::__construct();
        $this->load->database();
        
      
      //  $this->{$this->db_group_name} = $this->load->database($this->db_group_name, TRUE);
  
        }
        
             function get_serv_list()
            {
                //echo "test get_server_list";
                   $this->db->select('srv_name');
                   $query = $this->db->get("srv_mng");
                   $result= $query->result_array();
                   return $result;

            }
        
            function get_db_list($ser_name)
            {
               // echo $ser_name;
                $srv_cred=$this->get_ser_cred($ser_name);
               // print_r($srv_cred);
                $db_list=$this->get_db_lst($srv_cred,$ser_name);
            }
            
            function get_ser_cred($ser_name)
            {
               $this->db->where('srv_name',$ser_name);
               $query = $this->db->get('srv_mng');
               return ($query->result_array());
            }
            function get_db_lst($srv_cred,$ser_name)
            {
                //print_r($srv_cred);
                    foreach ($srv_cred as $key => $value) {
                  //  print_r($value);
                    $ip_add = $value['ip_add'];
                    $uname=$value['sql_uname'];
                    $pswd =$value['pswd'];
                  
                    }
                   $res= $this->connectDb($ip_add,$uname,$pswd);  
                   print_r($res);

            }

            function connectDb($ip_add,$uname,$pswd)
             {
                $config['hostname'] = $ip_add;
                $config['username'] = $uname;
                $config['password'] = $pswd;
                $config['database'] = '';
                $config['dbdriver'] = 'mysql';
                $config['dbprefix'] = '';
                $config['pconnect'] = TRUE;
                $config['db_debug'] = TRUE;
                $config['cache_on'] = FALSE;
                $config['cachedir'] = '';
                $config['char_set'] = 'utf8';
                $config['dbcollat'] = 'utf8_general_ci';
                $config['swap_pre'] = '';
                $config['autoinit'] = TRUE;
                $config['stricton'] = FALSE;
              
              $res =    $this->load->database($config,TRUE);
              $restab= $res->query("SHOW DATABASES");
              return($restab->result_array());

             }
}          

    ?&gt;