如何在CodeIgniter中访问DB配置?

时间:2022-10-28 10:52:23

In my application, I need to know what values are assigned to the DB config items such as database, username, etc. How do I access those information?

在我的应用程序中,我需要知道为数据库配置项分配了哪些值,例如数据库,用户名等。如何访问这些信息?

6 个解决方案

#1


1  

You can retrieve it with this: http://codeigniter.com/user_guide/libraries/config.html

您可以使用以下方法检索它:http://codeigniter.com/user_guide/libraries/config.html

$this->config->item('item name');

#2


37  

I don't have enough rep to comment on Matt Browne's correct answer but just adding a bit incase anyone forgets...

我没有足够的代表评论马特布朗的正确答案,但只是添加一点,任何人忘记...

load the db driver like so first:

首先加载db驱动程序:

$this->load->database();

then you can easily access what you need:

然后您可以轻松访问您需要的内容:

$this->db->hostname
$this->db->username
$this->db->password
$this->db->database

#3


9  

Pretty much all the config values are accessible via $this->db (take a look at system/database/DB_driver.php).

几乎所有的配置值都可以通过$ this-> db访问(看看system / database / DB_driver.php)。

That's what worked for me...none of the other suggestions here did.

这对我有用......这里没有其他建议。

#4


6  

In case you have multiple database connection groups defined in config/database.php, for eg :

如果您在config / database.php中定义了多个数据库连接组,例如:

  $db['dbname']['hostname'] = "localhost";
  $db['dbname']['username'] = "root";
  $db['dbname']['password'] = "root";
  $db['dbname']['database'] = "web_dbname";

  $db['dbname_readonly']['hostname'] = "localhost";
  $db['dbname_readonly']['username'] = "root";
  $db['dbname_readonly']['password'] = "root";
  $db['dbname_readonly']['database'] = "web_dbname_readonly";

If you want to use the connection params of any particular db in a controller or model:

如果要在控制器或模型中使用任何特定db的连接参数:

  $db = $this->load->database('dbname');

If you want to use in a helper or library :

如果要在帮助器或库中使用:

  $ci = &get_instance();
  $db = $ci->load->database('dbname');

The connection params will be available as $db->hostname, $db->username etc.

连接参数将以$ db-> hostname,$ db-> username等形式提供。

#5


0  

I stumbled across this, looking for a way to find all of the DB settings. Was not able to find a solution on-line, but found some useful code in system/database/DB.php

我偶然发现了这一点,寻找找到所有数据库设置的方法。无法在线找到解决方案,但在system / database / DB.php中找到了一些有用的代码

Here's my approach, get the contents of the entire database config:

这是我的方法,获取整个数据库配置的内容:

    if ( ! file_exists($f = APPPATH.'config/'.ENVIRONMENT.'/database.php')
        && ! file_exists($f = APPPATH.'config/database.php'))
    {
        show_error('The configuration file database.php does not exist.');
    }

    include($f);

    // Use a NEW variable.  
    // Because $db is a reserved name!!
    $db_settings = $db;

    foreach($db_settings as $key => $value) {
        // .. do something with .. $this->database->load($key);
        // .. do something with .. $value['database'];
        // .. do something with .. $value['password'];
    }

#6


-1  

You should be able to get at your configuration setting like this :

你应该能够得到这样的配置设置:

$this->config['env']

#1


1  

You can retrieve it with this: http://codeigniter.com/user_guide/libraries/config.html

您可以使用以下方法检索它:http://codeigniter.com/user_guide/libraries/config.html

$this->config->item('item name');

#2


37  

I don't have enough rep to comment on Matt Browne's correct answer but just adding a bit incase anyone forgets...

我没有足够的代表评论马特布朗的正确答案,但只是添加一点,任何人忘记...

load the db driver like so first:

首先加载db驱动程序:

$this->load->database();

then you can easily access what you need:

然后您可以轻松访问您需要的内容:

$this->db->hostname
$this->db->username
$this->db->password
$this->db->database

#3


9  

Pretty much all the config values are accessible via $this->db (take a look at system/database/DB_driver.php).

几乎所有的配置值都可以通过$ this-> db访问(看看system / database / DB_driver.php)。

That's what worked for me...none of the other suggestions here did.

这对我有用......这里没有其他建议。

#4


6  

In case you have multiple database connection groups defined in config/database.php, for eg :

如果您在config / database.php中定义了多个数据库连接组,例如:

  $db['dbname']['hostname'] = "localhost";
  $db['dbname']['username'] = "root";
  $db['dbname']['password'] = "root";
  $db['dbname']['database'] = "web_dbname";

  $db['dbname_readonly']['hostname'] = "localhost";
  $db['dbname_readonly']['username'] = "root";
  $db['dbname_readonly']['password'] = "root";
  $db['dbname_readonly']['database'] = "web_dbname_readonly";

If you want to use the connection params of any particular db in a controller or model:

如果要在控制器或模型中使用任何特定db的连接参数:

  $db = $this->load->database('dbname');

If you want to use in a helper or library :

如果要在帮助器或库中使用:

  $ci = &get_instance();
  $db = $ci->load->database('dbname');

The connection params will be available as $db->hostname, $db->username etc.

连接参数将以$ db-> hostname,$ db-> username等形式提供。

#5


0  

I stumbled across this, looking for a way to find all of the DB settings. Was not able to find a solution on-line, but found some useful code in system/database/DB.php

我偶然发现了这一点,寻找找到所有数据库设置的方法。无法在线找到解决方案,但在system / database / DB.php中找到了一些有用的代码

Here's my approach, get the contents of the entire database config:

这是我的方法,获取整个数据库配置的内容:

    if ( ! file_exists($f = APPPATH.'config/'.ENVIRONMENT.'/database.php')
        && ! file_exists($f = APPPATH.'config/database.php'))
    {
        show_error('The configuration file database.php does not exist.');
    }

    include($f);

    // Use a NEW variable.  
    // Because $db is a reserved name!!
    $db_settings = $db;

    foreach($db_settings as $key => $value) {
        // .. do something with .. $this->database->load($key);
        // .. do something with .. $value['database'];
        // .. do something with .. $value['password'];
    }

#6


-1  

You should be able to get at your configuration setting like this :

你应该能够得到这样的配置设置:

$this->config['env']