Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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';
share|improve this question
    
So after you import the database, you want to update your database.php file to match your localhost? –  bottleboot Jul 16 '13 at 9:11
add comment

1 Answer

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);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.