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',
  • 写回答

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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?