是否可以使用codeigniter中的特定文件为host
,dbname
,username
和password
设置变量?如果可能,如何在特定文件中设置变量连接也可以在config/database.php
中读取?我们知道CI使用config/database.php
来配置连接。
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:mypass@localhost/mydb?char_set=utf8&dbcollat=utf8_general_ci&cache_on=true&cachedir=/path/to/cache";
应用/配置/ config.php中
include ('config.php');
应用/配置/ 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',
...
);
仍然显示空白页面。
答案 0 :(得分:0)
是的,您可以在特定文件中设置数据库连接,如下所示:
$dsn = 'dbdriver://username:password@hostname/database?char_set=utf8&dbcollat=utf8_general_ci&cache_on=true&cachedir=/path/to/cache';
$this->load->database($dsn);
localhost的示例:
$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);
请参阅此codeigniter文档here。