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
    118
    Thanks
    22
    Thanked 0 Times in 0 Posts

    Codeigniter Controller SQL Load Error

    Hello when I use codeigniter and have small issue. When I click on my continue button on my form. I get an error when loading the sql file.

    PHP Code:
    Error Number1046
    No database selected
    CREATE TABLE 
    `rwd_country` ( `country_idint(11NOT NULL AUTO_INCREMENT, `namevarchar(128NOT NULL, `iso_code_2varchar(2NOT NULL, `iso_code_3varchar(3NOT NULL, `address_formattext NOT NULL, `postcode_requiredtinyint(1NOT NULL, `statustinyint(1NOT NULL DEFAULT '1'PRIMARY KEY (`country_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
    FilenameC:xampphtdocscodeigniter-redirect2systemdatabaseDB_driver.php
    Line Number
    330 
    But when I reload the page it loads it to the database.

    I know it is something for my form validation but I can not figure out what it is, I am stumbled on this.

    Controller: "Validation Section"

    PHP Code:
    $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 {
              
    $this->load->model('install/model_install');

    // Loads Into The Install Application Config Database
    if ($replace $this->model_install->database_install()) {
    $replace = array(
    'HOSTNAME' => $this->input->post('hostname'),
    'USERNAME' => $this->input->post('username'),
    'PASSWORD' => $this->input->post('password'),
    'DATABASE' => $this->input->post('database'),
    'DBDRIVER' => $this->input->post('dbdriver'),
    'DBPREFIX' => $this->input->post('dbprefix')
    );

    {
    $this->model_install->dump_sql();
    }
    }    

    // Loads Data Into Main Application Config Database
    if($replace $this->model_install->database_connect()) {
    $replace = array(
    'HOSTNAME' => $this->input->post('hostname'),
    'USERNAME' => $this->input->post('username'),
    'PASSWORD' => $this->input->post('password'),
    'DATABASE' => $this->input->post('database'),
    'DBDRIVER' => $this->input->post('dbdriver'),
    'DBPREFIX' => $this->input->post('dbprefix')

    );

    }

    redirect('install/step_4');

    Model:

    PHP Code:
    public function dump_sql() {

           
    $file APPPATH 'modules/install/config/database.sql';
            
            if (!
    file_exists($file)) { 
                exit(
    'Could not load sql file: ' $file); 
            }
                
            
    $lines file($file);
                
            if (
    $lines) {
                    
    $sql '';

            foreach(
    $lines as $line) {
                if (
    $line && (substr($line02) != '--') && (substr($line01) != '#')) {
                
    $sql .= $line;
          
                if (
    preg_match('/;\s*$/'$line)) {
                    
    $sql str_replace("DROP TABLE IF EXISTS `""DROP TABLE IF EXISTS `" $this->input->post('dbprefix'), $sql);
                    
    $sql str_replace("CREATE TABLE IF NOT EXISTS `""CREATE TABLE IF NOT EXISTS `" $this->input->post('dbprefix'), $sql);
                    
    $sql str_replace("CREATE TABLE `""CREATE TABLE `" $this->input->post('dbprefix'), $sql);
                    
    $sql str_replace("INSERT INTO `""INSERT INTO `" $this->input->post('dbprefix'), $sql);
                                
                    
    $this->db->query($sql);
            
                    
    $sql '';
                    }
                }
            }
            }
        }        

       public function 
    database_install() {
            
            
    $template file_get_contents(APPPATH 'modules/install/config/database.php');

            
    $replace = array(
            
    'HOSTNAME' => $this->input->post('hostname'),
            
    'USERNAME' => $this->input->post('username'),
            
    'PASSWORD' => $this->input->post('password'),
            
    'DATABASE' => $this->input->post('database'),
            
    'DBDRIVER' => $this->input->post('dbdriver'),
            
    'DBPREFIX' => $this->input->post('dbprefix')
            );    
           
            
    $template str_replace(array_keys($replace), $replace$template);
            

            
    $handle = @fopen(APPPATH 'config/database.php''w+');
                if (
    $handle !== false) {             
                    
    $response = @fwrite($handle$template);
                    
    fclose($handle);
                    if (
    $response) {
                    return 
    true;

                } else {

                     echo 
    'Error when process'; die;

                }
                
            }
        }

        public function 
    database_connect() {
            
            
    $template file_get_contents(APPPATH 'modules/install/config/database.php');

            
    $replace = array(
            
    'HOSTNAME' => $this->input->post('hostname'),
            
    'USERNAME' => $this->input->post('username'),
            
    'PASSWORD' => $this->input->post('password'),
            
    'DATABASE' => $this->input->post('database'),
            
    'DBDRIVER' => $this->input->post('dbdriver'),
            
    'DBPREFIX' => $this->input->post('dbprefix')
            );    
           
            
    $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;

                } else {

                     echo 
    'Error when process'; die;

                }
                
            }
        } 
    Founder Of Riwaka Website Designs
    http://www.riwakawebsitedesigns.com

  • #2
    Regular Coder
    Join Date
    Jan 2013
    Location
    Australia
    Posts
    118
    Thanks
    22
    Thanked 0 Times in 0 Posts
    Problem is now solved I had to remove the auto load database in install applications directory

    And change the function lay out on the install controller.
    PHP Code:
    if ($this->form_validation->run() == false) {
    $this->load->view('template/step_3'$data);
    } else {
    $this->model_install->load_app();
    $this->model_install->load_app_ins(); 
    if(
    $this->load->database()) {
    $this->model_install->load_sql();
    }
    redirect('step_4');

    Founder Of Riwaka Website Designs
    http://www.riwakawebsitedesigns.com


  •  

    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
    •