Welcome Guest, Not a member yet? Register   Sign In
Connecting to Multiple DBs - always last defined
#1

[eluser]Big E[/eluser]
I've read through all the multiple DB connect threads, gotten it down to the bare nitty gritty and it still doesn't work:

($db['smhc'] and $db['smhc_logs'] defined properly in database.php -- note: it doesn't matter which one is defined first in database.php; I've tested it both ways)

Code:
class Welcome extends CI_Controller {

  private $SMHC_DB;
  private $SMHC_LOGS_DB;

  function __construct()
  {
    parent::__construct();
    $this->SMHC_DB = $this->load->database('smhc',TRUE);
    $this->SMHC_LOGS_DB = $this->load->database('smhc_logs',TRUE);
  }

  function index()
  {
    $Query = $this->SMHC_LOGS_DB->query("SELECT * FROM log_t");
    $Row = $Query->row();
    var_dump($Row);
    exit;
  }

This works fine - SMHC_LOGS_DB was the last DB loaded. If I switch the 'smhc' and 'smhc_logs' database loads:

Code:
$this->SMHC_LOGS_DB = $this->load->database('smhc_logs',TRUE);
$this->SMHC_DB = $this->load->database('smhc',TRUE);

The 'SELECT' query fails because it's running on the wrong database. According to all the threads, it shouldn't matter in which order it was loaded because they should both be able to operate independently.

Is there a setting somewhere that I'm missing?

USING: Windows XP, Apache 2.2, PHP 5.2.13, Mysql 5.1.45, CodeIgniter 2.0 -- but it also doesn't work on Linux/Apache/PHP 5.3.5/Mysql 5.1.55/CI 2.0 either
#2

[eluser]InsiteFX[/eluser]
Maybe it thinks all your uppercase names are CONSTANTS!

InsiteFX
#3

[eluser]Big E[/eluser]
Nope, just lowercased the variables and still the same thing.

Surely, it has to be something to do with which database is "active". It's so strange because I see both DB variables defined, and the properties for each DB connection are correct.

It makes me think something deep in the bowels of CI is not paying attention to which DB is being referenced (and just using the last one defined) but surely others would be having this problem..?
#4

[eluser]Big E[/eluser]
I've tracked this even further, down through DB_driver.php/query() to simplequery() to mysql_driver.php/_execute().

I've verified that $this->conn_id is the same one that's being created when 'shmc_logs' DB connected is created, so basically, this call in mysql_driver.php:

Code:
return @mysql_query($sql, $this->conn_id);

is ignoring conn_id and just using whatever the last DB connection that was opened was.

What could/would cause this?
#5

[eluser]Big E[/eluser]
Solved (sorta). Changing 'pconnect' to FALSE in the database.php $db definitions fixed it. Some sort of cached DB connection thing.

Sorry for the bother everyone. Hopefully it helps someone else though..




Theme © iAndrew 2016 - Forum software by © MyBB