【发布时间】:2016-12-16 18:46:05
【问题描述】:
我需要您在 codeigniter 项目中的帮助。我想使用会话根据配置文件夹中的数据库文件动态更改数据库。 我在配置文件中做了 $db['default']['database'] = $_SESSION['db_name'], 但不起作用。 登录页面应该检查哪些其他数据库访问进行登录。
是否可以在选择数据库之前更改连接?
登录页面
public function login(){
if(!$this->session->userdata('id_funcionario') || !$this->session->userdata('logado')){
$this->db->database= "test";
$this->session->destroy();
$subdomain = $_SERVER['HTTP_HOST'];
$this->db->select("database");
$this->db->where("subdomain", $subdomain);
$access = $this->db->get("table")->row();
$this->session->set_userdata("database", $access->database);
$this->load->view('/geral/login');
}else{
$url = base_url('home');
header("Location: $url ");
}
}
登录页面有效!
AJAX 的数据发送到:
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class Ajax extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->helper('html');
$this->load->library('session');
}
public function toLog()
{
$email = $this->input->post('email');
$pass= md5($this->input->post('pass'));
$this->db->database = $this->session->userdata('database');
$this->db->select('*');
$this->db->from('users');
$this->db->where('usu_email',$email);
$this->db->where('usu_pass',$pass);
$usuario = $this->db->get()->result();
但是我不知道如何在查询时更改数据库的名称。我试过了,还是没变。
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Database Driver Class
*
* This is the platform-independent base DB implementation class.
* This class will not be called directly. Rather, the adapter
* class for the specific database will extend and instantiate it.
*
*/
class CI_DB_driver {
var $username;
var $password;
var $hostname;
var $database;
.......
/**
* Constructor. Accepts one parameter containing the database
* connection settings.
*
* @param array
*/
function __construct($params)
{
if (is_array($params))
{
foreach ($params as $key => $val)
{
$this->$key = $val;
}
}
$CI = & get_instance();
$this->database = $CI->session->userdata('database');
log_message('debug', 'Database Driver Class Initialized');
}....
你能帮帮我吗?
【问题讨论】:
标签: php ajax codeigniter session