2014-05-24 2 views
-1

codeigniter에서 SQL 파일을 실행하도록 모델을로드 할 때. 선택한 데이터베이스가 없다고 말하는 오류가 발생하지만 거기에 있습니다. 버튼을 클릭하여 다음 단계로 이동하면 오류가 발생합니다. 그것은 어떻게로드 SQL 전에 먼저 데이터베이스를로드해야합니다. 어떻게 작동하게하는지 잘 모르겠습니다.오류 번호 : 1046 데이터베이스가 선택되지 않았습니다 - CodeIgniter

페이지가 새로 고침되면 새로 고침됩니다. 당신은 MySQL을 말할 필요

오류 컨트롤러에

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 

$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');    
    $this->model_install->database_install(); 
    $this->model_install->database_connect(); 
    $this->model_install->dump_sql(); 
    redirect('step_4'); 

} 

모델 버튼을 클릭하는 즉시 수행해야 페이지를 새로

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

변경됩니다 드라이버로 SQLSRV 말처럼 뭔가를 사용하고 있습니까? 컨트롤러에? 모델에서? 자동로드 하시겠습니까? 모든 컨트롤러와 모델을 보여주십시오. – Craine

+0

예 모델이 있습니다. 첫 번째 게시물에 모델 함수를 추가했습니다. –

답변

0

나는 내 자신의 문제를 해결했습니다. 더 많은 조사를 한 후에 나는 자동 부하에 대한 데이터베이스를 설정했기 때문에 문제가있는 이유를 발견했습니다.

내 컨트롤러에서 문제를 해결하기 위해 $ this-> load-> database를 추가하고 sql 모델 함수를 포함 시켰습니다.

이 방법은 지금 내 모든 sql 파일을로드하므로 이제 해결되었습니다. 내 모델

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

이에 내 컨트롤러 기능 지수()

if ($this->form_validation->run() == false) { 
    $this->load->view('template/step_3', $data); 
} else { 
    $this->load->model('install/model_install'); 
    $this->model_install->database_install();   
    if($this->load->database()) { 
     $this->model_install->dump_sql(); 
     redirect('step_4'); 
     } else { 
      echo "Could Not Load SQL File"; die; 
    } 
} 

는 코드 WTH 문제가 아닙니다. 단순히 (C:\wamp\www\codeigniter\application\config) 'database.php'에서 데이터베이스 이름을 추가로 당신은이 DB 오류를 해결할 수 있습니다 -THE 코드는이 $db['default']['database'] = 'databasename';

같은 뭔가를해야만입니다 그리고 당신은이 오류가 자동로드 '에 또 한 줄을 추가 해결하기 위해 한 가지 더해야 .php '(C:\wamp\www\codeigniter\application\config) - 코드가 이와 같음 $autoload['libraries'] = array('database');

0

이 오류는 코드에 dsn 데이터베이스 형식이 사용 된 결과 발생합니다. 당신이 PDO 드라이버를 사용하여 MySQL의 데이터베이스에 연결하는 경우 DSN은 다음과 같습니다 :

$db['bank_sys'] = array(
    'dsn' => 'mysql:hostname=localhost;dbname=dba_bank_sys', 

DBNAME을가 언급 할 때 사용되는 것입니다 나는 내가 무엇을 의미하는 것은이 인 DSN이 올바르게 지정되지 않았습니다 이러한 오류가 발생 MySQL은 그러나 당신이 당신의 CI 설치에서에서 데이터베이스를로드하는 경우 다음 TEH DSN이

$db['bank_sys'] = array(
    'dsn' => 'sqlsrv:hostname=localhost;database=dba_bank_sys', 
관련 문제