在Codeigniter中未选择数据库

时间:2019-03-22 09:07:12

标签: php codeigniter

我已经使此函数从表单中获取值并覆盖config / database.php。

 function configure_database() {
        // write database.php
        $data_db = file_get_contents('./uploads/database.php');
       // session_start();
        $data_db = str_replace('%DATABASE%',    $_SESSION['dbname'],    $data_db);
        $data_db = str_replace('%USERNAME%',    $_SESSION['username'],  $data_db);
        $data_db = str_replace('%PASSWORD%',    $_SESSION['password'],  $data_db);
        $data_db = str_replace('%HOSTNAME%',    $_SESSION['hostname'],  $data_db);
        file_put_contents('./application/config/database.php', $data_db);
        $this->run_blank_sql();
      }

并成功overwrites config/database.php。在function的最后,我写了$this->run_blank_sql();,它调用了run_blank_sql(),我已经写了这个function以在SQL中运行uploads目录。

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

但这给了我这个错误。

enter image description here

configure_database()成功覆盖了这些值。

'hostname' => '%HOSTNAME%',
    'username' => '%USERNAME%',
    'password' => '%PASSWORD%',
    'database' => '%DATABASE%',
    'dbdriver' => 'mysqli',

收件人

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

2 个答案:

答案 0 :(得分:0)

无论您在哪里运行drop(我的代码中都没有看到)是问题所在。您要删除表,但未指定该表所在的架构。

即使只有一个名为category的表,也需要具体说明它所驻留的模式。将drop语句更改为:drop table schema.category

这与配置中的$db['default']['database'] = "YOUR_DB";没有任何关系。这只是标准的SQL

答案 1 :(得分:-1)

它在 enter image description here  null


我认为,您需要指定

No Database Selected

$db['default']['database'] = "YOUR_DB";