CodeIgniter Forums
Codeigniter ignoring $this->db->close() ? - 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: Codeigniter ignoring $this->db->close() ? (/thread-40840.html)



Codeigniter ignoring $this->db->close() ? - El Forum - 04-20-2011

[eluser]fiat[/eluser]
Hi There,

I'm having trouble with a database connection persisting even thou i call $this->db->close();

Below is my code

Code:
$beach_name = $this->Slsa_model->getBeachName($this->uri->segment(3));
    
    $data = array(
    'beach_key' => $this->uri->segment(3),
    'assessment_key' => $this->uri->segment(4),
    'name' => $beach_name
    );

        $this->load->database('postgres');
        $this->db->insert('beach', $data);
           $this->db->close();


Model

Code:
function getBeachName($beach_key)
    {
     $this->load->database('absamp');
         $sql = "SELECT * FROM beaches WHERE beach_key = ? ";
         $query = $this->db->query($sql, $beach_key);
        
        $row = $query->row_array();
           $this->db->close();

        return $row[beach_name];
    }

Now the issue is, when I try to perform the INSERT into POSTGRES database, i get the error message saying

Code:
ERROR: relation "beach" does not exist at character 13

After a bit of scratching around, it seems the insert is trying to apply to the ABSAMP database, not the POSTGRES

Why would this connection be persisting even thou I have closed the DB connection?


Codeigniter ignoring $this->db->close() ? - El Forum - 04-20-2011

[eluser]danmontgomery[/eluser]
IIRC it's because of the way CI handles loading. Even if the database is closed, the $this->database variable isn't unset, so the database isn't reloaded, even though you specify a different one. You should be able to get around it with:

Code:
$this->db = $this->load->database('absamp', TRUE);

Although, because they're separate databases I would probably separate them.

Code:
$this->_db = $this->load->database('absamp', TRUE);



Codeigniter ignoring $this->db->close() ? - El Forum - 04-20-2011

[eluser]fiat[/eluser]
Thanks for the quick reply, your fix resolved the issue perfectly.

So, does this count as a bug?


Codeigniter ignoring $this->db->close() ? - El Forum - 04-20-2011

[eluser]danmontgomery[/eluser]
Probably not... There's no clean way to "unload" anything in CI.