Welcome Guest, Not a member yet? Register   Sign In
New to CI - db object issue
#1

[eluser]Unknown[/eluser]
Hello everyone,

I'm working on a codeigniter project and there is a bug that i don't know how to resolve...
The error is :
Code:
A Database Error Occurred
Error Number: 1146
Table 'vocabulary.adm_one' doesn't exist
SELECT * FROM (`adm_one`) WHERE `category` = 1

On that project, i'm using 2 databases.
the table 'adm_one' is a part of the 'cocco' database and not 'vocabulary' (that's why i get the error)


Here is the config file :
Code:
$active_group = "cocco";
$active_record = TRUE;

$db['cocco']['hostname'] = "localhost";
$db['cocco']['username'] = "***";
$db['cocco']['password'] = "***";
$db['cocco']['database'] = "cocco";
$db['cocco']['dbdriver'] = "mysql";
$db['cocco']['dbprefix'] = "";
$db['cocco']['pconnect'] = TRUE;
$db['cocco']['db_debug'] = TRUE;
$db['cocco']['cache_on'] = FALSE;
$db['cocco']['cachedir'] = "";
$db['cocco']['char_set'] = "utf8";
$db['cocco']['dbcollat'] = "utf8_general_ci";


$db['vocabulary']['hostname'] = "localhost";
$db['vocabulary']['username'] = "***";
$db['vocabulary']['password'] = "***";
$db['vocabulary']['database'] = "vocabulary";
$db['vocabulary']['dbdriver'] = "mysql";
$db['vocabulary']['dbprefix'] = "";
$db['vocabulary']['pconnect'] = TRUE;
$db['vocabulary']['db_debug'] = TRUE;
$db['vocabulary']['cache_on'] = FALSE;
$db['vocabulary']['cachedir'] = "";
$db['vocabulary']['char_set'] = "utf8";
$db['vocabulary']['dbcollat'] = "utf8_general_ci";

I autoload the 'database' library. As i understood, '$this->db' will use the 'cocco' config.
To use the second database (aka vocabulary), i load it in the constructor of the model class when i need it, as followed :

Code:
<?php
class gonu_model extends Model {
    private $dbVoc;
    
    function gonu_model() {
        parent::Model(); //i assume that $this->db will be available and use 'cocco'
        $this->dbVoc = $this->load->database('vocabulary', TRUE);
    }
}
?>

The function which create that error, is a part of the gonu_model class :
Code:
function get_all_cat1() {
        //echo '<pre>';
        //var_dump($this->db);
        //echo '</pre>';
        $query = $this->db->get_where('adm_one', array('category'=>1));
        return $query->result();
    }

As you can see, i'm using $this->db and not $this->dbVoc.
I don't know why it's trying to use 'vocabulary' settings instead of 'cocco'.

When i var_dump() $this->db, i can see that "$this->db->database" is equal to cocco (which is right, and should work !)

I don't know why i get this error, nor how i can debug it.

If you have any advices which could help me, please share : D


Thanks, Pierre
(and sorry for my 'French' Wink )
#2

[eluser]Bart Mebane[/eluser]
According to the User Guide, under Connecting to Multiple Databases, you need to use a specific object name for each database. If you do it this way, it should work:
Code:
$this->dbVoc = $this->load->database('vocabulary', TRUE);
$this->dbCocco = $this->load->database('cocco', TRUE);
#3

[eluser]tonanbarbarian[/eluser]
actually the easiest way to handle this is with permissions in mysql
if the user account that you access the cocco database is given permission to access the vocabulary database as well then you can query the vocabulary database any time by prefixing the table name

Code:
SELECT * FROM (vocabulary.`adm_one`) WHERE `category` = 1




Theme © iAndrew 2016 - Forum software by © MyBB