[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 = '';
}
}
}
}
}