Php CodeIgniter多数据库连接未从第二个数据库获取数据

Php CodeIgniter多数据库连接未从第二个数据库获取数据,php,mysql,codeigniter,codeigniter-3,codeigniter-database,Php,Mysql,Codeigniter,Codeigniter 3,Codeigniter Database,一个是默认的,另一个是第二个DB,我只需要在几个控制器中使用它来维护通过系统发送的命令。下面是代码,我的database.php文件如下: 以下是我的配置: `$db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'carbiz', 'dbdriver' => 'mysqli'

一个是默认的,另一个是第二个DB,我只需要在几个控制器中使用它来维护通过系统发送的命令。下面是代码,我的
database.php
文件如下:

以下是我的配置:

`$db['default'] = array(
'dsn'   => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'carbiz',
'dbdriver' => 'mysqli',
'dbprefix' => 'web_',
'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['abcd'] = array(
'dsn'   => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'sale2',
'dbdriver' => 'mysqli',
'dbprefix' => 'web_',
'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
);`
这是我的控制器

public function __construct()
 {
    parent::__construct();
    $this->load->model('admin_model');
    $this->otherdb = $this->load->database('abcd', TRUE);`
 }
现在在使用第二个db的同一控制器中的一个函数中

function add_scondary()
 {   
     $val=$this->otherdb->select('title')->get('products');
     print_r("here");
     print_r($val); 
 }

const CI_VERSION='3.1.7'

希望这对您有所帮助:

首先,您必须像这样定义一个
public
private
变量
$otherdb

public $otherdb; 
public function __construct()
{
   parent::__construct();
   $this->otherdb = $this->load->database('abcd', TRUE);
}  


function add_scondary()
 {    
     $data = $this->otherdb->get('products')->result();
     print_r(data);
 }
使用相同连接的不同数据库的更好方法是:

如果只需要在同一连接上使用不同的数据库,则不需要创建单独的数据库配置。您可以在需要时切换到其他数据库,如下所示:


出现了什么错误?无它只是在上面的代码布尔值上打印从函数调用到成员函数result()的“here”确保变量u define与$this->variable\u name匹配;请显示您的代码
$this->db->db_select('sale2')$数据['abcd']=$this->db->get('products')->result();打印(“此处”);打印($data['abcd'])这是我已删除的代码加载\u数据库代码您的表名是什么它是
products
还是
web\u products
$this->db->db_select('sale2');
$data['abcd'] = $this->db->get('products')->result();