2017-04-13 35 views
0

可以使用codeigniter中的特定文件设置变量host,dbname,usernamepassword?如果可能,如何在特定文件中设置变量连接也可以在config/database.php中读取?我们知道CI使用config/database.php来配置连接。如何设置连接变量与CodeIgniter中的特定文件

EDITED

这是代码:

rootweb/index.php的

$this->config->load('config', TRUE); 

rootweb/config.php中

$config['host']="localhost"; 
$config['dbname']="mydb"; 
$config['username']="myusername"; 
$config['password']="mypass"; 

我也尝试使用这样的:

$config['dsn']="postgre://myusername:[email protected]/mydb?char_set=utf8&dbcollat=utf8_general_ci&cache_on=true&cachedir=/path/to/cache"; 

的application/config/config.php文件

include ('config.php'); 

的application/config/database.php中

$host = $this->config->item('host', 'config'); 
$dbname = $this->config->item('dbname', 'config'); 
$username = $this->config->item('username', 'config'); 
$password = $this->config->item('password', 'config'); 
$dsn = $this->config->item('dsn', 'config'); 

$db['default'] = array(
    'dsn' => $dsn, 
    'hostname' => $host, 
    'username' => $username, 
    'password' => $password, 
    'database' => $dbname, 
    'dbdriver' => 'postgre', 
    ... 
); 

而且仍然呈现空白页。

+1

发布你到目前为止所做的事 –

+0

你在做什么?仔细阅读codeigniter手册。如何创建配置和创建配置文件的位置。 – Gaurav

回答

0

是的,你可以这样设置在特定的文件的数据库连接:

本地主机
$dsn = 'dbdriver://username:[email protected]/database?char_set=utf8&dbcollat=utf8_general_ci&cache_on=true&cachedir=/path/to/cache'; 
$this->load->database($dsn); 

例子:

$dsn = 'mysqli://root:@localhost/test?char_set=utf8&dbcollat=utf8_general_ci&cache_on=false&cachedir='; //If do not want cache 
$this->load->database($dsn); 

请参阅本笨文档here

+0

感谢您的帮助。假设我将一些变量设置为'globalconfig.php'。然后用index.php中的'require'globalconfig.php''轻松添加它,但只显示空白页面。 –

+0

你必须把'globalconfig.php'在这样的笨呼叫配置文件:'$这个 - > config->负载( 'globalconfig',TRUE);' '$主机= $这个 - > config-> item('host','globalconfig');' – Gaurav

+0

我编辑了我的问题,试着按照你的建议。请再看一遍。 –

相关问题