代码点火器:CI_DB_mysqli_driver无法转换为字符串

问题描述:

I have a existing users database from another database and want to use it on my new site using codeigniter with a new database both is running on mysql, i configured my database.php like below, and configure another database connection.

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => 'root',
    'database' => 'new_database',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

$db['otherdb'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => 'root',
    'database' => 'members_database',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

Now i am trying to query the username and password on my Users_model.php my function is like this.

public function login($username, $password){

            $otherdb = $this->load->database('otherdb', TRUE); 
            $this->$otherdb->where('user_name', $username);
            $this->$otherdb->where('user_pass', $password);



            $result = $this->$otherdb->get('members');

            if($result->num_rows() == 1){

              return $result->row(0)->ID;


            }else{

              return false;
            }

          }

But got a error below, i am new with codeigniter and not sure if this is the proper way of querying from another database, any advice would help! thanks in advance!

Object of class CI_DB_mysqli_driver could not be converted to string

Change this $otherdb = $this->load->database('otherdb', TRUE); To $this->otherdb = $this->load->database('otherdb', TRUE);

It will work.