【问题标题】:How to set connection variable with specific file in CodeIgniter如何在 CodeIgniter 中使用特定文件设置连接变量
【发布时间】:2017-09-08 23:51:18
【问题描述】:

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

已编辑

这是代码:

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";

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',
    ...
);

并且仍然显示空白页。

【问题讨论】:

  • 发布你到目前为止所做的事情
  • 你在做什么?仔细阅读 codeigniter 手册。如何创建配置以及在哪里创建配置文件。

标签: php codeigniter


【解决方案1】:

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

$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 文档here

【讨论】:

  • 感谢您的帮助。假设我将一些变量设置为globalconfig.php。然后在 index.php 中使用require 'globalconfig.php' 轻松附加它,但只显示空白页。
  • 你必须把globalconfig.php和调用配置文件在codeigniter这样的:$this->config->load('globalconfig', TRUE);$host = $this->config->item('host', 'globalconfig');
  • 我已经编辑了我的问题,试着听从你的建议。请再看一遍。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-08
  • 2021-10-19
  • 2023-04-06
  • 2020-12-02
  • 1970-01-01
  • 2012-09-15
  • 2019-11-13
相关资源
最近更新 更多