0

I have a script in my controller which is as follows:

if($this->db->query("CREATE DATABASE IF NOT EXISTS db_ecommerce"))
        {
                $sql=file_get_contents('./databse_backup/backup.sql');
                foreach (explode(";\n", $sql) as $sql) 
                {
                    $sql = trim($sql);
                    //echo  $sql.'<br/>============<br/>';
                        if($sql) 
                    {
                        $this->db->query($sql);
                    } 
                }  
        }

It creates a database and then runs a backup sql file.

My issue is that we need to configure the config/database.php file beforehand.

First I want this script to run and after that the database.php file should be changed.

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

Since its now for localhost hence,

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'db_name';
1
  • So after you import the database, you want to update your database.php file to match your localhost? Commented Jul 16, 2013 at 9:11

1 Answer 1

2

An approach would be to have 'template file' in your installation folder like: ex. database.php

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = '<?php echo $hostname;?>';
$db['default']['username'] = '<?php echo $username;?>';
$db['default']['password'] = '<?php echo $password;?>';
$db['default']['database'] = '<?php echo $database;?>';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '<?php echo $prefix;?>';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

and then you can get the values for db connection (from a form for example) and write the new file in your codeigniter application (application/config/database.php)

ex.

$settings['hostname']   = $this->input->post('hostname');
$settings['username']   = $this->input->post('username');
$settings['password']   = $this->input->post('password');
$settings['database']   = $this->input->post('database');
$settings['prefix']     = $this->input->post('prefix');     

$file_contents      = $this->load->view('templates/database', $settings, true);


write_file($_SERVER['DOCUMENT_ROOT'].'application/config/database.php', $file_contents);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.