Welcome Guest, Not a member yet? Register   Sign In
[Solved] SQL Not Load Unless Refresh Page
#1

[eluser]riwakawd[/eluser]
Hello I have a model which loads my SQL file but when I click on continue, it comes up with an error.

Error Number: 1046
No database selected
CREATE TABLE `country` ( `country_id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(128) NOT NULL, `iso_code_2` varchar(2) NOT NULL, `iso_code_3` varchar(3) NOT NULL, `address_format` text NOT NULL, `postcode_required` tinyint(1) NOT NULL, `status` tinyint(1) NOT NULL DEFAULT '1', PRIMARY KEY (`country_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
Filename: C:\xampp\htdocs\codeigniter\system\database\DB_driver.php
Line Number: 330

But when I F5 reload page it loads OK. It should load straight away once click on continue.

Controller:

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

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

$this->model_install->dump_sql();
redirect('install/step_4');
    
}

Model Function Of SQL

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($line, 0, 2) != '--') && (substr($line, 0, 1) != '#')) {
$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 `", "CREATE TABLE `" . $this->input->post('dbprefix'), $sql);
$sql = str_replace("INSERT INTO `", "INSERT INTO `" . $this->input->post('dbprefix'), $sql);
      
$this->db->query($sql);
  
$sql = '';
}
}
}
}
}
#2

[eluser]Massaki[/eluser]
You must set which database you are working
Just add " use 'database_name' " before any other command
#3

[eluser]InsiteFX[/eluser]
Code:
$this->db->reconnect();
#4

[eluser]riwakawd[/eluser]
[quote author="InsiteFX" date="1400897882"]
Code:
$this->db->reconnect();
[/quote]

I am getting there now but now will not load sql

Code:
if ($this->form_validation->run() == false) {
      
   $this->load->view('template/step_3', $data);
  
   } else {
    
   $this->load->model('install/model_install');
   $this->load->dbutil();
   if($this->model_install->database_install()) {
    $this->model_install->database_connect();
    if($this->dbutil->database_exists($this->input->post('database'))) {
     $this->model_install->dump_sql();
     redirect('install/step_4');
    }
    
   } else {
    $this->load->view('template/step_3', $data);
   }  
  }
#5

[eluser]riwakawd[/eluser]
Problem Now Solved

I had done it this way. And I removed my auto load database works this way to run sql from Model

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




Theme © iAndrew 2016 - Forum software by © MyBB