7

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.

2
  • how to put $this->dbforge->create_database('db2');. I don't have idea to do this. can I know? I have put this in controllers/migrate.php in index function but I always get error. thanks. Commented Jan 6, 2015 at 14:31
  • @pmgrace Are you using cpanel into your server or anything else please? Please add the server information so that other may understand at which environment does it work? Commented Jun 9, 2022 at 4:18

6 Answers 6

8
$this->db->query('use db2');

$this->db->query('use DB1');
Sign up to request clarification or add additional context in comments.

Comments

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;
   }
  }

}

Comments

1

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.

1 Comment

This is a good option for CI3 and above. P.S. CI4 syntax: $this->myforge = \Config\Database::forge('other_db');
0

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

1 Comment

I've done this but I get the error...Undefined property: CI_DB_mysql_driver::$dbforge
0

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.

Comments

0

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…

1 Comment

This answer is technically correct, but does not answer the question. This has no effect on which database the dbforge uses. This does not change the database for the default database in use by dbforge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.