首页 > 解决方案 > 在 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();
      }

并且它成功overwritesconfig/database.php。在function我写的最后一个$this->run_blank_sql();调用中,run_blank_sql()我写了这个function来运行SQLinuploads目录。

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

但它给了我这个错误。

在此处输入图像描述

configure_database()成功覆盖这些值。

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

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

标签: phpcodeigniter

解决方案


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

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

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


推荐阅读