CodeIgniter & DBForge - Create Database and Tables

后端 未结 7 2041
小蘑菇
小蘑菇 2020-12-16 05:34

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 tabl

7条回答
  •  天命终不由人
    2020-12-16 05:59

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

提交回复
热议问题