CodeIgniter 多数据库连接不从第二个数据库中获取数据
CodeIgniter multiple db connection not fetching data from second 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);`
}
现在在使用第二个数据库的同一个控制器中的一个函数中
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);
}
更好的方法 以相同的连接使用不同的数据库:
如果您只需要在同一连接上使用不同的数据库,则无需创建单独的数据库配置。您可以在需要时切换到不同的数据库,如下所示:
$this->db->db_select('sale2');
$data['abcd'] = $this->db->get('products')->result();
一个是默认数据库,另一个是第二个数据库,我只需要在少数控制器中维护通过系统的订单。这是代码,我的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);`
}
现在在使用第二个数据库的同一个控制器中的一个函数中
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);
}
更好的方法 以相同的连接使用不同的数据库:
如果您只需要在同一连接上使用不同的数据库,则无需创建单独的数据库配置。您可以在需要时切换到不同的数据库,如下所示:
$this->db->db_select('sale2');
$data['abcd'] = $this->db->get('products')->result();