0

I'm trying to accomplish the following functionality: I have a main connection to a database (inside application/config/database.php: db['default']), and that connections work, it retrieves data, I want to use that data to establish a new connection to a different DB.

Is that possible or I need to create an external PDO connection to handle it?

(I'm using Codeigniter 3.1.0)

Lvkz
  • 946
  • 14
  • 20

3 Answers3

3

With the help of Touheed Khan I was able to reach the solution of my problem, posting it here because I had to do some workaround of the suggested solution to make it work. The following is how my solution is working right now:

Controller.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Controller extends CI_Controller {
 public $dynamicDB;

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

 public function index() {
  //Somehow retrieve the following information from user.
  $host;
  $user;
  $pass;
  $dbname;
  $port;
  // End of retrieval information from user.

  $this->dynamicDB = array(
   'hostname' => $host,
   'username' => $user,
   'password' => $pass,
   'database' => $dbname,
   'dbdriver' => 'postgre',
   'dbprefix' => '',
   'pconnect' => FALSE,
   'db_debug' => TRUE,
   'port' => $port
  );

  $this->load->model('data_model');
  $result = $this->data_model->select($this->dynamicDB, 
                                      $this->get('id'));
  var_dump($result);
 }
}

Data_model.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Data_model extends CI_Model {
 function __construct(){
  parent::__construct();
 }

 public function returnQuery($query) {
  if ($query->num_rows() > 0) {
   return $query->result();
  } else {
   return array();
  }
 }

 public function select($dynamicDB, $id) {
  $dynamicDB = $this->load->database($dynamicDB, TRUE);

  $dynamicDB->select('*');
  $dynamicDB->where('id', $id);
  $dynamicDB->from('table');

  $query = $dynamicDB->get();
  return $this->returnQuery($query);
 }
}
Lvkz
  • 946
  • 14
  • 20
  • 1
    Nicely implemented buddy... you can move dynamicDB declaration to helper that will give your reusability If you want to use this in any other controller in current application. Thanks ! – Touheed Khan Oct 24 '16 at 07:14
  • Thanks for the tip!! Will do!! – Lvkz Oct 24 '16 at 14:20
0

You can add array to /config/database.php

Like this article http://tutsnare.com/connecting-multiple-database-in-codeigniter/ And also in CodeIgniter Documentation https://www.codeigniter.com/user_guide/database/connecting.html#connecting-to-multiple-databases

Edit

Maybe you need this post codeigniter database configuration programmatically form user input

Community
  • 1
  • 1
  • As exposed on the previous answer, the problem is that I don't have the connection data until I make the second connection. So I can't set the second one beforehand. How do I set the new DB configuration from the inside controller. – Lvkz Oct 22 '16 at 05:42
  • So do you need to keep the original plus the new database. Reading through the Codeigniter Documentation, gives you many options on how to do anything you want... – TimBrownlaw Oct 22 '16 at 05:48
0

$this->db2 = $this->load->database('seconddb', TRUE);    // 'seconddb' is a connection array key in database config like 'default'. 

Refer this answer here for more information, repox has explained everything in detail.

If more help needed, I'm happy to help.

Thanx, Happy Coding.

Community
  • 1
  • 1
Touheed Khan
  • 2,149
  • 16
  • 26
  • Ok I get it, but my problem is in how do I pass the new data to the database.php file? Because I have worked with different databases before, but the database.php file does a autoload before I have the data for the new connection I can't pass the data "on time". – Lvkz Oct 22 '16 at 05:37
  • 1
    @Lvkz : You can pass whole config array to $this->load->database() function. Refer this answer for details http://stackoverflow.com/a/25459842/4419992 – Touheed Khan Oct 22 '16 at 05:51
  • Hey @Touheed Khan, your last comment pointed me in the right direction! Will add an answer with the details of my findings. – Lvkz Oct 23 '16 at 05:26