Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 452
pmgrace Avatar asked May 11 '13 19:05

pmgrace


People also ask

Is CodeIgniter front end or backend?

Design company and web development with CodeigniterFrom our agency, we create backend and frontend with Codeigniter, a PHP framework that makes it easier to use the MVC (Model View Controller) mechanism for the creation of control panels.

Which is better Laravel or CodeIgniter?

Ultimately, Laravel is a better framework than CodeIgniter majorly due to the coding pattern which is the most favored for its elegant look. Along with this, it also supports robust application development in no time.

What is the difference between PHP and CodeIgniter?

Codeigniter is a PHP-driven framework that includes a bag of libraries, plug-ins, and tools, simplify the PHP syntax and streamline the PHP code alongside underpinning the MVC approach. Its advantages comprise of hassle-free migration, easier configuration, tractability and addition of new functionality.

What can CodeIgniter do?

CodeIgniter is open-source software used to develop the web framework that is to be put to use to create dynamic web pages and websites in the PHP language. It is considered one of the best open sources by many industrialists and experts.


2 Answers

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

$this->db->query('use DB1');
like image 119
Ben Stonehouse Avatar answered Sep 19 '22 04:09

Ben Stonehouse


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

}
like image 45
Mrudul Avatar answered Sep 18 '22 04:09

Mrudul