Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

php - CodeIgniter & DBForge - Create Database and Tables

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
$this->db->query('use db2');

$this->db->query('use DB1');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...