问题
I'm trying to write a script in CodeIgniter that will create a database and then will add tables to that newly created database along with various fields in to the new table.
So far I've got dbforge to create the new database but since I'm using the database library and have pre-set database connection values, when it goes to create a table, it puts it in the pre-selected database and not the one it just created.
In the database.php config file I have the following:
$db['default']['database'] = 'db1';
If I use the following command to create a new database:
$this->dbforge->create_database('db2');
'db2' will get created but then the following command puts the table in 'db1'.
$this->dbforge->create_table('table1');
I need 'table1' created in 'db2'. How do I get CI to select the newly created database ('db2') to create the table in the correct place, and then switch back to 'db1'?
I've looked at the following question which is similar to what I'm doing but I do not want to have to put any further connection entries in the database.php
Codeigniter showing error: No database selected
Any help appreciated!
EDIT - I should add that the create_database could have any name for the database name... This is a script to automatically create a database and the relevant tables where the DB name is pulled from a form.
回答1:
$this->db->query('use db2');
$this->db->query('use DB1');
回答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;
}
}
}
回答3:
as your default database in config is db1 create_table method will always create table in selected database, after creating db2 you need to select db2 to create table in that database you can do in following way
$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();
for more information please check CI user guide CI user Guide Connecting Database
回答4:
change database connection of codeigniter migration
I've used the suggestion provided in the above question to resolve my problem. After I create the second database I used the following command to switch to it:
$this->db->query('use db2');
This lets me create the tables in db2. Then used the following to switch back to the default database:
$this->db->query('use DB1');
Works exactly how I want it.
回答5:
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
回答6:
A better way is to get a dbforge class representing the database you want to manipulate
$this->myforge = $this->load->dbforge($this->other_db, TRUE);
Use the $this->myforge
object to work as usual.
This method will avoid conflicts when building libraries. It's best not to mess with the default database of your library users at run time.
回答7:
When you want to use 2 Database on project then first you need to define bothe database as -
$DB1 = $this->load->database('group_one', TRUE);
$DB2 = $this->load->database('group_two', TRUE);
When you connect this way, you will use your object name to issue commands rather than the syntax used throughout this guide. In other words, rather than issuing commands with:
$this->db->query();
$this->db->result();
etc…
You will instead use:
$DB1->query();
$DB1->result();
etc…
来源:https://stackoverflow.com/questions/16500733/codeigniter-dbforge-create-database-and-tables