错误号码1046:在Codeigniter中未选择数据库

时间:2019-03-22 15:27:59

标签: php codeigniter

我为function run_blank_sql()创建了Installing的SQL文件的Database。 但是这段代码$this->load->database();给了我一个错误。 No database selected

我也有autoloadedDatabase$autoload['libraries'] = array('database','session');

database也连接到config/database.php

'hostname' => 'localhost',
'username' => 'root',
'password' => '********',
'database' => 'waqas',
'dbdriver' => 'mysqli',

database也在Mysql中创建。

  function run_blank_sql() {
        $this->load->database();
        // Set line to collect lines that wrap
        $templine = '';
        // Read in entire file
        $lines = file('./uploads/install.sql');
        // Loop through each line
        foreach ($lines as $line) {
          // Skip it if it's a comment
          if (substr($line, 0, 2) == '--' || $line == '')
            continue;
          // Add this line to the current templine we are creating
          $templine .= $line;
          // If it has a semicolon at the end, it's the end of the query so can process this templine
          if (substr(trim($line), -1, 1) == ';') {
            // Perform the query
            $this->db->query($templine);
            // Reset temp variable to empty
            $templine = '';
          }
        }
      }

1 个答案:

答案 0 :(得分:0)

我认为您尚未为项目选择任何默认数据库。这就是为什么您会收到此错误。为了选择数据库,您应该至少指定一个。主要是将数据库加载到构造函数内部。因此,请在下面修改您的代码。

$this->db = $this->load->database("default",TRUE);
相关问题