0

I have seen this topic and tried to implement that way and unfortunatelly isn't working.

I need to dynamically connect to a database with CodeIgniter. This database cannot be a constant variable in the file application/config/databases.php. I have tried two different ways of connecting to a different database:

The first one is by model - with third parameter as documentation refers.

class users extends CI_Controller
{
   public function __construct()
   {
      parent::_construct();

      $config['hostname'] = 'localhost';
      $config['username'] = 'root';
      $config['password'] = '';
      $config['database'] = 'clockin_AlKj';
      $config['dbdriver'] = 'mysqli';
      $config['dbprefix'] = '';
      $config['pconnect'] = FALSE;
      $config['db_debug'] = TRUE;

      $this->load->model('company_model', '', $config);
      $this->company = $this->company_model->get();
   }
}

This throws the error:

Error Number: 1146

Table 'clockin_admin.company' doesn't exist

SELECT * FROM (company)

Filename: C:\xampp\htdocs\clockin\system\database\DB_driver.php

The second way is to connect by using database.

 class users extends CI_Controller
 {
    public function __construct()
    {
       parent::_construct();

       $this->load->model('company_model');
       $this->company = $this->company_model->get();
    }
 }

class Company_model extends CI_model
{
   private $otherDb;

   public function __construct()
   {
      parent::__construct();

      $config['hostname'] = 'localhost';
      $config['username'] = 'root';
      $config['password'] = '';
      $config['database'] = 'clockin_AlKj';
      $config['dbdriver'] = 'mysqli';
      $config['dbprefix'] = '';
      $config['pconnect'] = FALSE;
      $config['db_debug'] = TRUE;

      $this->otherDb = $this->load->database($config);
   }

   public function get()
   {
      $this->otherDb->get('company')->row();
   }
}

This throws the error:

Fatal error: Call to a member function get() on a non-object in C:\xampp\htdocs\clockin\application\models\company_model.php on line 45

Which refers to the line $this->otherDb->get('company')->row();

The problem is that if I verify if the connection was established it also returns error.

if($this->load->database($config) === FALSE)
   echo 'Yes, I could not connect..';

Finally, my configuration of file database.php is the following:

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

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'clockin_admin';
$db['default']['dbdriver'] = 'mysqli';
$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;

3 Answers 3

1

Sorry i gave you the wrong answer at first. i just overlook at CodeIgniter code and tough it might work. Here's an alternate solution:

You can create an db on the fly by using a 'connection string' as first parameter, and TRUE as second parameter so instead of replacing the $this->db var it will just return the DB object to $this->otherDb:

$this->otherDb = $this->load->database('mysql://username:password@hostname:9090/database',TRUE);  

it should work as intented

Additional if you want to set dbprefix, pconnect or db_debug, you can do it by defining the query part of the 'connection string':

$this->otherDb = $this->load->database('mysql://username:password@hostname:9090/database?dbprefix=pre_&pconnect=FALSE&db_debug=TRUE',TRUE);

source: https://ellislab.com/codeigniter/user-guide/database/connecting.html

Sign up to request clarification or add additional context in comments.

5 Comments

It still not working. It continue to focus the database set in the database.php file. $this->otherDb = $this->load->database('mysql://root:@localhost:9090/clockin_AlKj',TRUE);
Are you sure? I have just tested and this method works. perhaps your password string contain some symbols that may confuse the parser? also, you server is really running on port 9090? if it's the default port you don't need to specify it.
Did you reverted the get method to $this->otherDb->get('company')->row(); ?
Yes I did, and I don't have password (I'm working on localhost for now). The port of MySQL in XAMPP is 3306, I also tried that port. I'm not using differente subdomains, but I came across with this solution stackoverflow.com/questions/20785134/… and I'm going to try it.
My project will allow multiple databases but on the same domain.
1

You can change the config after you load the database:

class Company_model extends CI_model
{
  private $otherDb;

  public function __construct()
  {
    parent::__construct();

    $this->load->database();

    $this->db->hostname = 'localhsot';
    $this->db->username = 'root';
    $this->db->password = '';
    $this->db->database = 'clockin_AlKj';
    $this->db->dbprefix = '';
    $this->db->pconnect = FALSE;
    $this->db->db_debug = TRUE;
  }

  public function get()
  {
     $this->db->get('company')->row();
  }
}

You just can't change the driver that way.

2 Comments

I just made the changes and it continues to point to the clockin_admin database instead of clockin_AlKj. The error that is given is the same I post as first error.
moving the $this->load->database() line below the other statements should work it out.
1

Please Try this its working for lasted version of mysqli

$this->otherDb = $this->load->database('mysqli://username:password@hostname:3306/database',TRUE);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.