douvcpx6526
2017-06-15 07:17
浏览 42
已采纳

Codeigniter使用外部文件替换database.php

I have two web sites using codeigniter that uses the same database configurations(hostname,username,password), just want to ask if the database configuration can be called from a external file? The thing is if i want to change my database root password i have to change the two sites database.php manually, now lets says i have 10 websites this will be a difficult task correct? i just want to change a single file so all sites can use that configuration.

I am using mysql via PHPmyadmin.

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => 'root',
    'database' => 'MyBlog',
    'dbdriver' => 'mysqli',

图片转代码服务由CSDN问答提供 功能建议

我有两个使用codeigniter的网站,使用相同的数据库配置(主机名,用户名,密码),只是想 询问是否可以从外部文件调用数据库配置? 问题是如果我想更改我的数据库root密码我必须手动更改两个站点database.php,现在让我说我有10个网站这将是一项艰巨的任务是否正确? 我只想更改单个文件,以便所有站点都可以使用该配置。

我通过PHPmyadmin使用mysql。

  $ db  ['default'] = array(
'dsn'=>'',
'hostname'=>'localhost',
'用户名'=>'root',
'密码'=&gt  ;'root',
'数据库'=>'MyBlog',
'dbdriver'=>'mysqli',
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dtvnbe1428 2017-06-15 09:01
    最佳回答

    If you have need to use one configuration for all sites, you can make the file (i.e. db_config.php) and set ti somewhere to be available to all CI applications.

    db_config.php

    <?php
    
    return [
        'dsn'   => '',
        'hostname' => 'localhost',
        'username' => '',
        'password' => '',
        'database' => '',
        'dbdriver' => 'mysqli',
        'dbprefix' => '',
        'pconnect' => FALSE,
        'db_debug' => (ENVIRONMENT !== 'production'),
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array(),
        'save_queries' => TRUE
    ];
    

    Then in your APPPATH.'config/db.php' of every application you should have:

    $db['default'] = require('/path/to/db_config.php');
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题