我想从两个不同的数据库表中检索数据,例如作业数据库和分类数据库,在那两个数据库中我有job_history表和ad_mobile_phones表现在我想在单个控制器上从这两个表中检索数据 请建议我 提前谢谢
$active_group = 'default';
$active_record = TRUE;
//for job portal
$db['default']['hostname'] = 'TZ07';
$db['default']['username'] = 'root';
$db['default']['password'] = 'TZ';
$db['default']['database'] = 'doolaly_jobz';
$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;
//for classified portal
$db['classified_db']['hostname'] = 'TZ07';
$db['classified_db']['username'] = 'root';
$db['classified_db']['password'] = 'TZ';
$db['classified_db']['database'] = 'doolaly';
$db['classified_db']['dbdriver'] = 'mysql';
$db['classified_db']['dbprefix'] = '';
$db['classified_db']['pconnect'] = TRUE;
$db['classified_db']['db_debug'] = TRUE;
$db['classified_db']['cache_on'] = FALSE;
$db['classified_db']['cachedir'] = '';
$db['classified_db']['char_set'] = 'utf8';
$db['classified_db']['dbcollat'] = 'utf8_general_ci';
$db['classified_db']['swap_pre'] = '';
$db['classified_db']['autoinit'] = TRUE;
$db['classified_db']['stricton'] = FALSE;
控制器
class Account_detail extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Jobseekermodel','',TRUE);
$CI = &get_instance();
$this->db = $CI->load->database('classified_db', TRUE);
//$secound_db= $this->load->database('classified_db',TRUE);
$this->load->model('Emailmodel','',TRUE);
$this->load->model('Bookmodel','',TRUE);
$this->load->model('Consultantmodel','',TRUE);
$this->load->model('Openingmodel','',TRUE);
$this->load->model('Commfuncmodel','',TRUE);
$this->lang->load('message', 'english');
$this->lang->load('mail', 'english');
$this->load->library('form_validation');
if (session_id() == "") session_start();
}
}
答案 0 :(得分:0)
在config / database.php中建立新连接
第一个db(主数据库): -
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'doolaly_jobz';
$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: -
$db['db2']['hostname'] = 'localhost';
$db['db2']['username'] = 'root';
$db['db2']['password'] = '';
$db['db2']['database'] = 'doolaly';
$db['db2']['dbdriver'] = 'mysql';
$db['db2']['dbprefix'] = '';
$db['db2']['pconnect'] = FALSE;
$db['db2']['db_debug'] = TRUE;
$db['db2']['cache_on'] = FALSE;
$db['db2']['cachedir'] = '';
$db['db2']['char_set'] = 'utf8';
$db['db2']['dbcollat'] = 'utf8_general_ci';
$db['db2']['swap_pre'] = '';
$db['db2']['autoinit'] = TRUE;
$db['db2']['stricton'] = FALSE;
然后在控制器中加载第二个数据库,如
$CI = &get_instance();
$this->db2 = $CI->load->database('db2', TRUE); //doolaly
并获取类似
的查询$this->db2->query('your_query') //doolaly
$this->db->query('your_query') // doolaly_jobz
答案 1 :(得分:0)
you can manually connect to db each time you need or you can autoload it...
set like this in your config.php
$config['hostname'] = 'localhost';
$config['username'] = 'root';
$config['password'] = '';
$config['database'] = 'doolaly';
$config['dbdriver'] = 'mysql';
$config['dbprefix'] = '';
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = '';
$config['char_set'] = 'utf8';
$config['dbcollat'] = 'utf8_general_ci';
$config['swap_pre'] = '';
$config['autoinit'] = TRUE;
$config['stricton'] = FALSE;
add the desired fields and connect the db using
$this->load->database($config);
or try this
$dsn = 'dbdriver://username:password@hostname/database';
$this->load->database($dsn);
it will works...