Welcome Guest, Not a member yet? Register   Sign In
Can some one help me with this database connectiong issue
#7

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

             }
}          

    ?>


Messages In This Thread
Can some one help me with this database connectiong issue - by El Forum - 09-02-2013, 10:54 PM



Theme © iAndrew 2016 - Forum software by © MyBB