dou12352 2011-08-30 18:45
浏览 50

运行时的CodeIgniter数据库连接

I'm still having troubles with manually connection to a database on runtime. I open a new question becuase didn't find the answer yet.

Here is the case,

I connect to the database with a connection user to validate a username and password that is entered with a form. Then with this new information, if the user is valid I have to connect to the database with this new username and password. Looks contain the following:

config/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;

Then in the login controller: 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');       
}

Everything goes ok within this controller but when redirect is perform (that loads different models), e.g., tareas_model.php I keep on receiving the same error on the first lines:

models/tareas_model.php

function __construct()
{
    parent::__construct();
    $query = $this->db->query('ALTER SESSION SET nls_sort = \'SPANISH\'');
}

The error is:

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

I also tried setting $this->db as a session variable but I keep receiving the same error and I'm driving pretty crazy. I've tried everything that I found online with no luck.

Any help?

  • 写回答

2条回答 默认 最新

  • doudou1309 2011-09-06 15:56
    关注

    Well, for those who have run under this problem I think I found the solution. I modified the files as follows:

    config/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;
    

    Then, the controller that first receives POST username and password as: controllers/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');   
        }
    
    }
    

    Then, in the models: 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;
    }
    

    Then in every controller I reload the db like:

    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');
    }
    

    Minor where just a few in each controller and none in the models so, it was great!

    Cheers, V

    评论

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答