簡體   English   中英

如何在CodeIgniter中使用特定文件設置連接變量

[英]How to set connection variable with specific file in 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',
    ...
);

並仍顯示空白頁。

是的,您可以在特定文件中設置數據庫連接,如下所示:

$dsn = 'dbdriver://username:password@hostname/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);

在此處參考此codeigniter文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM