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
inuploads
目录。
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',
解决方案
无论您在哪里运行drop
(我在您的代码中没有看到)都是问题所在。您正在删除一个表,但没有指定该表所在的架构。
即使您只有一个表被调用category
,您也需要具体说明它所在的模式。将您的drop
语句更改为:drop table schema.category
。
这与配置中的 没有任何关系$db['default']['database'] = "YOUR_DB";
。这只是标准 SQL
推荐阅读
- javascript - 在反应本机导航中调用导航道具方法时获取“未定义不是对象”
- javascript - iframe 正在破坏浏览器的后退按钮
- php - 如何遍历 PHP 上的产品和类别列表
- amazon-web-services - 通过架构更改在 S3 和 Redshift 之间卸载和重新加载数据
- matlab - 用底函数定义周期函数
- google-cloud-platform - Firebase 动态链接在 Twitter 和 LinkedIn 中被标记为可疑
- accessibility - 屏幕阅读器和浏览器事件
- c++ - 将函数作为可变参数模板参数传递,并调用它们
- python - 自动礼物的循环问题
- c# - 从 VS 构建中的 Release/Debug 文件夹中删除 DLL