我正在尝试从Codeigniter中的第二个数据库进行查询,因此在我的数据库配置中,我有以下内容:
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'gallery';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$db['tc']['hostname'] = 'localhost';
$db['tc']['username'] = 'root';
$db['tc']['password'] = '';
$db['tc']['database'] = 'timecard';
$db['tc']['dbdriver'] = 'mysql';
$db['tc']['dbprefix'] = '';
$db['tc']['pconnect'] = TRUE;
$db['tc']['db_debug'] = TRUE;
$db['tc']['cache_on'] = FALSE;
$db['tc']['cachedir'] = '';
$db['tc']['char_set'] = 'utf8';
$db['tc']['dbcollat'] = 'utf8_general_ci';
$db['tc']['swap_pre'] = '';
$db['tc']['autoinit'] = TRUE;
$db['tc']['stricton'] = FALSE;
在我的模型方法中,我有以下代码:
public function getAll()
{
$tc = $this->load->database('tc', TRUE);
$sql = "SELECT username, MIN(timeIn)
FROM timecard2.tc_timecard
GROUP BY userid having MIN(timeIn) >? and
MIN(timeIN)<? order by MiN(timeIN);";
$q=$tc->query($sql,'2013-01-08','2013-01-23');
if ($q->num_rows() > 0)
{
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
但它不起作用!它是查询第二个数据库的正确方法吗?
如果您需要更多说明,请告诉我您需要更多澄清的部分。
编辑: 刚刚发现它可以正常使用吗? (参数)! 它适用于以下代码:
$sql = "SELECT username, MIN(timeIn)
FROM tc_timecard
GROUP BY userid having
MIN(timeIn) > ? and MIN(timeIN) < '2013-01-23'
order by MiN(timeIN);";
$q=$tc->query($sql,'2013-01-08');
你有什么线索为什么它没有得到第二个参数?
由于
答案 0 :(得分:1)
根据codeigniter的文档:https://www.codeigniter.com/user_guide/database/connecting.html
$tc = $this->load->database($db['tc'], TRUE);