我仍然遇到在运行时手动连接数据库的麻烦。我打开一个新问题因为还没有找到答案。
就是这样,
我使用连接用户连接到数据库,以验证使用表单输入的用户名和密码。然后使用这个新信息,如果用户有效,我必须使用这个新的用户名和密码连接到数据库。外观包含以下内容:
配置/ database.php中
$active_record = TRUE;
$db['default']['hostname'] = 'host';
$db['default']['username'] = 'username';
$db['default']['password'] = 'pwd';
$db['default']['database'] = 'dbname';
$db['default']['dbdriver'] = 'oci8';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'WE8ISO8859P1';
$db['default']['dbcollat'] = '';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = FALSE;
$db['default']['stricton'] = FALSE;
然后在登录控制器中:controllers / login.php
public function __construct(){
parent::__construct();
$this->output->enable_profiler(TRUE);
$this->CI =& get_instance();
$this->db_con = $this->CI->load->database('default', FALSE, TRUE);
$this->load->model('Login_model');
session_start();
}
public function index(){
if (isset($_POST['ingresar'])){
$d['data'] = $this->Login_model->Obtener_usuario($_POST['usuario']);
$_SESSION['logueo'] = $d['data']->row();
if (empty($_SESSION['logueo'])){
$d['error'] = 2;
}
else{
if (is_null($_SESSION['logueo']->USER_ORACLE)){
$d['error'] = 1;
}
if (!is_null($_SESSION['logueo']->N_SECUENCIA)){
cargar_var_sesion($_POST['usuario'], $_SESSION['logueo']->USER_ORACLE, $_SESSION['logueo']->N_SECUENCIA, $_SESSION['logueo']->C_USR_NOM_APE);
$db['limited']['hostname'] = 'samehost';
$db['limited']['username'] = $_POST['usuario'];
$db['limited']['password'] = $_POST['clave'];
$db['limited']['database'] = 'samedbname';
$db['limited']['dbdriver'] = 'oci8';
$db['limited']['dbprefix'] = '';
$db['limited']['pconnect'] = FALSE;
$db['limited']['db_debug'] = TRUE;
$db['limited']['cache_on'] = FALSE;
$db['limited']['cachedir'] = '';
$db['limited']['char_set'] = 'WE8ISO8859P1';
$db['limited']['dbcollat'] = '';
$db['limited']['swap_pre'] = '';
$db['limited']['autoinit'] = FALSE;
$db['limited']['stricton'] = FALSE;
$active_group = 'limited';
$this->db = $this->CI->load->database('limited', FALSE, TRUE);
redirect('inicio', 'location');
}
else{
$d['error'] = 2;
}
}
}
else{
$d['error'] = 0;
}
$this->load->view('/componentes/header');
$this->load->view('/componentes/menu_sin_login');
$this->load->view('/login/login', $d);
$this->load->view('/componentes/footer');
}
在此控制器中一切正常但是当执行重定向(加载不同的模型)时,例如tareas_model.php我在第一行继续收到相同的错误:
模型/ tareas_model.php
function __construct()
{
parent::__construct();
$query = $this->db->query('ALTER SESSION SET nls_sort = \'SPANISH\'');
}
错误是:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Inicio::$db
Filename: core/Model.php
Line Number: 50
Fatal error: Call to a member function query() on a non-object in /var/www/crm/application/models/tareas_model.php on line 8
我也尝试将$ this-> db设置为会话变量,但我一直收到同样的错误而且我开车很疯狂。我尝试过网上找到的所有东西都没有运气。
任何帮助?
答案 0 :(得分:1)
那么,对于那些在这个问题下运行的人,我想我找到了解决方案。我按如下方式修改了文件:
配置/ database.php中
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = '';
$db['default']['dbdriver'] = 'oci8';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'WE8ISO8859P1';
$db['default']['dbcollat'] = '';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = FALSE;
$db['default']['stricton'] = FALSE;
然后,首先接收POST用户名和密码的控制器为: 控制器/ login.php中
public function __construct()
{
parent::__construct();
$this->output->enable_profiler(TRUE);
session_start();
}
public function index()
{
if (isset($_POST['ingresar'])){
if (!empty($_POST['usuario']) && !empty($_POST['clave'])){
$c['hostname'] = "myhost";
$c['username'] = strtoupper($_POST['usuario']);
$c['password'] = $_POST['clave'];
$c['database'] = "dbname";
$c['dbdriver'] = "oci8";
$c['dbprefix'] = "";
$c['pconnect'] = TRUE;
$c['db_debug'] = TRUE;
$c['cache_on'] = FALSE;
$c['cachedir'] = "";
$c['char_set'] = "WE8ISO8859P1";
$c['dbcollat'] = "";
$active_record = TRUE;
$_SESSION['c'] = $c;
$this->db = $this->load->database($_SESSION['c'], TRUE, TRUE);
$this->load->model('Login_model');
$d['data'] = $this->Login_model->Obtener_usuario($_POST['usuario']);
$_SESSION['logueo'] = $d['data']->row(); //print_r($_SESSION['logueo']);
cargar_var_sesion($_SESSION['logueo']->C_USUARIO_WEB, $_SESSION['logueo']->USER_ORACLE, $_SESSION['logueo']->C_USR_NOM_APE, $this->db->conn_id, $_SESSION['c']);
redirect('inicio', 'location');
}
else{
$d['error'] = 1;
$this->load->view('/componentes/header');
$this->load->view('/componentes/menu_sin_login');
$this->load->view('/login/login', $d);
$this->load->view('/componentes/footer');
}
}
else{
$d['error'] = 0;
$this->load->view('/componentes/header');
$this->load->view('/componentes/menu_sin_login');
$this->load->view('/login/login', $d);
$this->load->view('/componentes/footer');
}
}
然后,在模型中:models / login_model.php
function __construct()
{
parent::__construct();
$CI =& get_instance();
}
function Obtener_usuario($usuario)
{
$CI =& get_instance();
$query_logueo = $CI->db->query('SELECT....');
return $query_logueo;
}
然后在每个控制器中我重新加载数据库,如:
public function __construct()
{
parent::__construct();
$this->output->enable_profiler(TRUE);
session_start();
$this->db = $this->load->database($_SESSION['c'], TRUE, TRUE);
$this->load->model('Any_model');
}
轻微,每个控制器中只有少数几个,模型中没有,所以很棒!
干杯, V
答案 1 :(得分:1)
这是针对更改运行时数据库配置....似乎假设您正在从url中捕获主机名....并且根据您必须更改数据库配置...
第一GT&;
class yourcontrollername extends CI_Controller
{
public function index($do='')
{
//declare seesion or catch a url parameter's value in session
if(isset($_GET['qual'])) //Qual is parameter name which is supplied in url
{
session_start();
$_SESSION['Company'] = $_GET['qual'];
}
public function delx($id)
{
session_start();
$Scompany = $_SESSION['Company'];
$this->load->helper('url');
$server_url = site_url('api');
$this->load->library('xmlrpc');
$this->xmlrpc->server($server_url, 80);
$this->xmlrpc->method('delx_order_delete');
$request = array();
$request = array("id"=>$id , "Param"=>$Scompany);
$this->xmlrpc->request(array(array($request,'struct')));
$this->load->library('session');
$sess = array();
if ( ! $this->xmlrpc->send_request()){
$sess['err'] = $this->xmlrpc->display_error();
}else{
$resl = $this->xmlrpc->display_response();
if($resl['failure'] != ''){
$sess['err'] = $resl['failure'];
}
if($resl['success'] != ''){
$sess['succ'] = $resl['success'];
$output = $this->session->userdata('output');
if(is_array($output)){
foreach($output as $key=>$valo){
if($valo['ID'] == $id){
unset($output[$key]);
}
}
}
}
$this->session->set_userdata($sess);
$this->session->set_userdata(array('output'=>$output));
}
}
}
}
现在,在Api.model上
function x_order_delete($params)
{
$compname = $params['Param'];
if($compname == 'xyz.con')
{
$c['hostname'] = $compname;
$c['username'] = "username";
$c['password'] = "password";
$c['database'] = "db name";
$c['dbdriver'] = "mysql";
$c['dbprefix'] = "";
$c['pconnect'] = TRUE;
$c['db_debug'] = TRUE;
$c['cache_on'] = FALSE;
$c['cachedir'] = '';
$c['char_set'] = 'utf8';
$c['dbcollat'] = 'utf8_general_ci';
$c['swap_pre'] = '';
$c['autoinit'] = TRUE;
$c['stricton'] = FALSE;
$_SESSION['c'] = $c;
$this->db = $this->load->database($_SESSION['c'], TRUE, TRUE);
//$this->load->database();
if($query = $this->db->query('DELETE FROM table
Where ID = '.$params['id'])){
return array("Successfully deleted record.","");
} else {
return array("","Error deleting record.");
}
return $query;
}
}
就是这样....我希望这会对你有所帮助....祝你好运