从 Codeigniter 中的 database.php 页面获取数据库详细信息

发布于 2024-12-28 17:30:11 字数 436 浏览 3 评论 0原文

我如何从database.php页面获取值? 我的意思是我们可以使用 base_url() 访问基本 url。在 config.php 页面中设置的基本 url 如下所示:

$config['base_url'] = 'example.com';

database.php 中的数据库详细信息如下:

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'username';
$db['default']['password'] = 'password';
$db['default']['database'] = 'database_name';

我想访问以下中的此详细信息我的控制器页面。我该怎么做?

How can i get value from database.php page?
I mean we can access base url by using base_url().The base url set in config.php page like this :

$config['base_url'] = 'example.com';

The database details in database.php is like:

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'username';
$db['default']['password'] = 'password';
$db['default']['database'] = 'database_name';

I want to access the this details in my controller page.How can i do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云胡 2025-01-04 17:30:12
<?php
include('database.php');
echo $db['default']['hostname'];
?>  
<?php
include('database.php');
echo $db['default']['hostname'];
?>  
巷子口的你 2025-01-04 17:30:12

还有另一种可行的方法:将数据库设置用作配置数据。

您可以通过将以下行附加到database.php 文件并使用您选择的过滤来做到这一点:

// get all configurations
$config = $db;

// be more specific
foreach($db as $key => $settings)
{
    $config[$key]['hostname'] = $settings['hostname'];
    // etc
}

然后您可以像通常加载配置值一样加载数据:

$this->load->config('database',TRUE);
$db = $this->config->item('default','database');
echo $db['hostname'];
// etc

There is another way that would work: you make the db settings available as config data.

You can do that by appending the following lines to the database.php file and use the filtering of your choice:

// get all configurations
$config = $db;

// be more specific
foreach($db as $key => $settings)
{
    $config[$key]['hostname'] = $settings['hostname'];
    // etc
}

Then you can load the data as you normally would load config values:

$this->load->config('database',TRUE);
$db = $this->config->item('default','database');
echo $db['hostname'];
// etc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文