Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jan 2013
    Location
    Australia
    Posts
    120
    Thanks
    22
    Thanked 0 Times in 0 Posts

    Question DB Forge Codeigniter Create Database Not Working

    Hello I am just having trouble creating a new database will not create a new one when I fill out my inputs on my form. Can not seem to find much useful information on creating new database with dbforge in codeigniter

    Controller
    PHP Code:
    class Step_3 extends MX_Controller {
       
       public function 
    index() {
         
    $this->load-> library('form_validation');
         
    $this->lang->load('install/english''english');
            
         
    // DB Installer
         
    $this->form_validation->set_rules('hostname''Hostname');
         
    $this->form_validation->set_rules('username''Database Username''trim|required|xss_clean');
         
    $this->form_validation->set_rules('password''Database Password');
         
    $this->form_validation->set_rules('database''Database''required');
         
    $this->form_validation->set_rules('dbdriver''Database Driver');
         
    $this->form_validation->set_rules('dbprefix''Database Prefix');
         
    $data['button_continue'] = $this->lang->line('button_continue');
         
    $data['button_back'] = $this->lang->line('button_back');

          if (
    $this->form_validation->run() == false) {

          
    $this->load->view('template/step_3'$data);

           } else {
                
               if (
    $this->dbforge->create_database($this->database)){
                
    $this->load->model('install/model_install');
                
    $this->model_install->database_connect();
             
    $config['hostname'] = $this->hostname;
             
    $config['username'] = $this->username;
             
    $config['password'] = $this->password;
             
    $config['database'] = $this->database;
             
    $config['dbdriver'] = $this->dbdriver;
             
    $config['dbprefix'] = $this->dbprefix;
             
    $config['pconnect'] = TRUE;
             
    $config['db_debug'] = TRUE;
             
    $config['cache_on'] = FALSE;
             
    $config['cachedir'] = "";
             
    $config['char_set'] = "utf8";
             
    $config['dbcollat'] = "utf8_general_ci";
             
    $config['swap_pre'] = '';
             
    $config['autoinit'] = TRUE;
             
    $config['stricton'] = FALSE;

             
    $this->database=$this->load->database($config,TRUE);
             
    $this->load->database();
         
    $this->db=$this->database;
         
    $this->load->dbforge();

         
    redirect('install/step_4');

            } else {

               
    $this->load->view('template/step_3'$data);

            }


       }


    PHP Code:
    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class 
    Model_install extends CI_Model {

            public function 
    __construct()
            {
            
    $this->hostname $config['db']['hostname'];
            
    $this->username $config['db']['username'];
            
    $this->password $config['db']['password'];
            
    $this->database $config['db']['database'];
            
    $this->prefix $config['db']['prefix'];
            }

            public function 
    database_connect() {

            
    $template file_get_contents(APPPATH 'modules/install/config/database.php');

            
    $replace = array(
            
    'HOSTNAME' => $this->hostname,
            
    'USERNAME' => $this->username,
            
    'PASSWORD' => $this->password,
            
    'DATABASE' => $this->database,
            
    'DBDRIVER' => $this->dbdriver,
            
    'DBPREFIX' => $this->prefix
            
    );

            
    $template str_replace(array_keys($replace), $replace$template);

            
    $handle = @fopen(dirname(FCPATH) . 'application/config/database.php''w+');

                if (
    $handle !== false) {
                    
    $response = @fwrite($handle$template);
                    
    fclose($handle);

                    if (
    $response) {

                    return 
    true;

                }

            }
        }

  • #2
    Regular Coder
    Join Date
    Jan 2013
    Location
    Australia
    Posts
    120
    Thanks
    22
    Thanked 0 Times in 0 Posts
    Topic is solved.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •