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
$this->db->query('use db2');
$this->db->query('use DB1');
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;
}
}
}
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.
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
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…
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