如何在CodeIgniter中使用特定文件设置连接变量

时间:2017-04-13 02:06:42

标签: php codeigniter

是否可以使用codeigniter中的特定文件为hostdbnameusernamepassword设置变量?如果可能,如何在特定文件中设置变量连接也可以在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',
    ...
);

仍然显示空白页面。

1 个答案:

答案 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