我正在尝试为从我的 codeigniter
站点注册的每个客户创建一个数据库和表,我正在查看 dbforge
文档并尝试了下面的代码。
但是出现了错误,我想让它对每个客户都是动态的,所以将它放在 config/database.php 上不可用,有没有办法防止这种情况? 我是 codeigniter 的新手,不确定下面的代码是否也正确。
You have specified an invalid database connection group (customer_LYWA) in your config/database.php file.
public function create_customer_database($code){
$database = $this->dbutil->database_exists('customer_'.$code);
if($database){
return "";
}else{
$this->dbforge->create_database('customer_'.$code);
$fields = array(
'blog_id' => array(
'type' => 'INT',
'constraint' => 5,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'blog_title' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'blog_author' => array(
'type' =>'VARCHAR',
'constraint' => '100',
'default' => 'King of Town',
),
'blog_description' => array(
'type' => 'TEXT',
'null' => TRUE,
),
);
$this->dbforge->add_field($fields);
$this->dbforge->add_key('blog_id', TRUE);
// gives PRIMARY KEY (blog_id)
$this->dbforge->add_key('blog_title');
// gives KEY (blog_title)
$otherdb = $this->load->database('customer_'.$code, TRUE);
$this->dbforge->create_table('blog');
return "created";
}
}
最佳答案
我认为您正在尝试为每个客户创建一个表。
因此,我认为您将数据库与表混淆了。数据库包含表,因此您需要连接到config/database.php中的正确数据库,并使用脚本在该数据库内创建表。
您无需为每个客户创建新数据库。这将使事情比应有的困难 100 倍,并且非常难以维护。
关于php - Codeigniter 在 config/database.php 之外创建数据库和表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49626246/