John Davidson

php - get database config settings to controller/views in codeigniter

0 comments
Message:


I am using Codeigniter 3 and I got a page to download a backup .sql file (its working well).The problem is i need to change the config both on config/database.php and db.php if i upload my project into server.So what i want is to only change the config/database.php when upload to the server


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 );

controller :


  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');
}
}

i just tried with this method but its obviously wrong :


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


My Answer:
'',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'mydatabase',
'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
);

In order to achieve this, you can create a separate file (e.g. db.php) in your config folder that contains the database configuration settings. Then, you can load this file in your controller or views to access the database configuration settings.

Here's how you can do it:

1. Create a new file named db.php in your config folder with the following content:

php
defined('BASEPATH') OR exit('No direct script access allowed');

$config['db_hostname'] = 'localhost';
$config['db_username'] = 'root';
$config['db_password'] = '';
$config['db_database'] = 'mydatabase';
$config['db_driver'] = 'mysqli';
$config['db_charset'] = 'utf8';
$config['db_collation'] = 'utf8_general_ci';


2. In your controller or view file, load the db.php configuration file and access the database configuration settings like this:

php
$this->config->load('db', TRUE);
$db_config = $this->config->item('db', 'db');

$hostname = $db_config['db_hostname'];
$username = $db_config['db_username'];
$password = $db_config['db_password'];
$database = $db_config['db_database'];
$driver = $db_config['db_driver'];
$charset = $db_config['db_charset'];
$collation = $db_config['db_collation'];

// Now you can use these variables in your database connection settings


By doing this, you only need to change the database configuration settings in the db.php file when uploading your project to the server, and the rest of your code can remain unchanged.

Rate this post

3 of 5 based on 1339 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us