由于某些奇怪的原因,当我尝试加载数据库时,没有返回任何结果。
class Customer_model extends CI_Model {
public function fetch_email_list() {
$DB1 = $this->load->database('orders', TRUE);
if ( $this->load->database('orders') === FALSE ){
echo 'no database';
}
$results = $DB1->query("SELECT * FROM email_list");
return $results->result_array();
}
}
我已经检查了我的数据库配置
$db['orders']['hostname'] = 'localhost';
$db['orders']['username'] = 'db_user';
$db['orders']['password'] = 'password';
$db['orders']['database'] = 'db_name';
$db['orders']['dbdriver'] = 'mysql';
$db['orders']['dbprefix'] = '';
$db['orders']['pconnect'] = FALSE;
$db['orders']['db_debug'] = TRUE;
$db['orders']['cache_on'] = FALSE;
$db['orders']['cachedir'] = '';
$db['orders']['char_set'] = 'utf8';
$db['orders']['dbcollat'] = 'utf8_general_ci';
$db['orders']['swap_pre'] = '';
$db['orders']['autoinit'] = TRUE;
$db['orders']['stricton'] = FALSE;
我如何解决?
答案 0 :(得分:1)
database.php
中未添加任何数据库。如果我们只有多个数据库,则使用$DB1 = $this->load->database('orders', TRUE);
。
设置默认数据
$db['orders']['database'] = 'orders';
代码
public function fetch_email_list() {
$this->load->database();
$query = $this->db->query("SELECT * FROM email_list");
$result = $query->result_array();
return $result
}
答案 1 :(得分:0)
您可以在模型中全局定义数据库实例,以便您可以跨方法访问它,如下所示:
class Some_model extends CI_Model {
// Our 2nd database
protected $DB2;
public function __construct () {
parent::__construct();
$this->DB2 = $this->load->database('orders', TRUE);
}
public function some_method () {
$q = $this->DB2->query('...');
}
public function some_other_method () {
$q = $this->DB2->query('...');
}
}
答案 2 :(得分:0)
试试这个
$query = $this->db->get('email_list');
return $query->result_array();