在 codeigniter 中将数据库配置设置为 controller/views

get database config settings to controller/views in codeigniter

我正在使用 Codeigniter 3,我得到了一个下载备份 .sql 文件的页面(它运行良好)。问题是我需要更改 config/database 上的配置。php 和 db.php 如果我将我的项目上传到 server.So 我想要的是仅在上传到服务器

时更改 config/database.php

config/database.php :

$db['default'] = array(
'dsn'   => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'my_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
);

views/db.php :

$mysqlUserName      = "root";
$mysqlPassword      = "";
$mysqlHostName      = "localhost";
$DbName             = "my_database";
$backup_name        = "mybackup.sql";

Export_Database($mysqlHostName,$mysqlUserName,$mysqlPassword,$DbName,  $tables=false, $backup_name=false );

控制器:

  public function backup()
  {
   if ($this->data['user_profile']['user_group'] > 2)
  {
   return show_404('The page you requested was not found.');
  }else {
   $this->load->view('db');

   redirect('admin/setting/backup','refresh');
   }
  }

我刚试过这个方法,但显然是错误的:

$mysqlUserName      = $db['username'];
$mysqlPassword      = $db['password'];
$mysqlHostName      = $db['hostname'];
$DbName             = $db['database'];

在这里,在你的控制器中使用这个:

public function index()
{
    $this->load->database();  //load the driver first
    $mysqlUserName      = $this->db->username;
    $mysqlPassword      = $this->db->password;
    $mysqlHostName      = $this->db->hostname;
    $DbName             = $this->db->database;
}

如果你有多个数据库,你可以使用$this->load->database('dbname_you_want_to_use')

您可以使用 $this->config->item($item [ $index='']) 方法获取控制器或视图的配置值。在 documentation.

中了解更多信息

示例:

$mysqlUserName      = $this->config->item('username', 'default');
$mysqlPassword      = $this->config->item('password', 'default');
$mysqlHostName      = $this->config->item('hostname', 'default');
$DbName             = $this->config->item('database', 'default');