我正在尝试在CodeIgniter中编写一个脚本,该脚本将创建一个数据库,然后将表添加到新创建的数据库以及新表中的各个字段。
到目前为止,我已经有了dbforge来创建新数据库,但由于我正在使用数据库库并预先设置了数据库连接值,因此在创建表时,它会将其放入预先选择的数据库中而不是它刚创造的那个。
在database.php配置文件中,我有以下内容:
$db['default']['database'] = 'db1';
如果我使用以下命令创建新数据库:
$this->dbforge->create_database('db2');
'db2'将被创建,但随后的命令将表放入'db1'。
$this->dbforge->create_table('table1');
我需要在'db2'中创建'table1'。如何让CI选择新创建的数据库('db2')以在正确的位置创建表,然后切换回'db1'?
我查看了以下与我正在做的类似的问题,但我不想在database.php中添加任何其他连接条目
Codeigniter showing error: No database selected
任何帮助表示赞赏!
编辑 - 我应该补充一点,create_database可以有数据库名称的任何名称...这是一个自动创建数据库的脚本以及从中提取数据库名称的相关表格形式。
答案 0 :(得分:7)
$this->db->query('use db2');
$this->db->query('use DB1');
答案 1 :(得分:2)
if ($this->dbforge->create_database('my_db_test'))
{
try{
$current_database = "my_db_test";
$this->db->database = $current_database;
$this->db->close();
$config['hostname'] = "localhost";
$config['username'] = "root";
$config['password'] = "";
$config['database'] = $current_database;
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
$this->load->database($config);
$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);
$this->dbforge->create_table('ipn_log', TRUE);
}catch(Exception $e){
echo $e->getMessage();die;
}
}
}
答案 2 :(得分:0)
作为配置中的默认数据库是db1 create_table方法将始终在所选数据库中创建表,在创建db2之后,您需要选择db2以在该数据库中创建表,您可以按以下方式执行
$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "db2";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
$db2 = $this->load->database($config,TRUE);
$db2->dbforge->create_table('table1');
//if you want to close connection
$db2->db->close();
有关详细信息,请查看CI用户指南CI user Guide Connecting Database
答案 3 :(得分:0)
change database connection of codeigniter migration
我使用了上述问题中提供的建议来解决我的问题。创建第二个数据库后,我使用以下命令切换到它:
$this->db->query('use db2');
这让我可以在db2中创建表。然后使用以下内容切换回默认数据库:
$this->db->query('use DB1');
完全符合我的要求。
答案 4 :(得分:0)
Base Controller step by step created
Step 1 :- go to codeigniter folder and go to core folder
Step 2 :- go to core folder and created file name this(MY_controller.php)
step 3 :-
For Example this like
Note :Why CI_Controller extends beacuase CI_Controller beacuase CI_Controller features used
<?php
class MY_Controller extends CI_Controller
{
}
?>
This used to all controller used not created __construct() write
CI_controller featcures used this class MY_Controller
CI_Controller inheritance MY_Controller
step 4 :- go to application,Controller Welcome
<?php
class Welcome extends MY_Controller
{
}
?>
Note :MY_Controller inheritance CI_Controller
step 5 :- created admin.php controller and User controller created
Public Controller created this
Articles List
Articles View
Admin Controller created this
Login
Logout
Add Article
Edit Article
Delete Article
<?php
class User extends MY_Controller
{
}
?>
and created public.php controller created
<?php
class public extends MY_Controller
{
public function index()
{
echo "public index";
}
}
?>
Note :Controller Created here extends CI_Controller
Why Base Controller Created in Codeigniter ?
ans-> Fox example __construct()created
any load and used any controller that is base controller
What is CI_Controller ?
-->CI_Controller any codeigniter features used extends CI_Controller
答案 5 :(得分:0)
更好的方法是获取一个代表您要操作的数据库的dbforge类
$this->myforge = $this->load->dbforge($this->other_db, TRUE);
使用$this->myforge
对象照常工作。
此方法将避免在构建库时发生冲突。最好不要在运行时弄乱库用户的默认数据库。
答案 6 :(得分:0)
当您要在项目上使用2数据库时,首先需要将bothe数据库定义为-
$ DB1 = $ this-> load-> database('group_one',TRUE);
$ DB2 = $ this-> load-> database('group_two',TRUE);
以这种方式连接时,将使用对象名来发出命令,而不是使用本指南中使用的语法。换句话说,而不是使用以下命令发出命令:
$ this-> db-> query();
$ this-> db-> result();
等...
您将改为使用:
$ DB1-> query();
$ DB1-> result();
等...