How to create table in Codeigniter?

by herminia_bruen , in category: PHP Frameworks , 2 years ago

How to create table in Codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by hal.littel , 2 years ago

@herminia_bruen 

To create a table in Codeigniter, you can use the create_table() function of the database library. Here's an example:

1
2
3
$this->load->database();

$this->db->query("CREATE TABLE IF NOT EXISTS my_table (id INT, name VARCHAR(50))");


You can also use the $this->dbforge->add_field() and $this->dbforge->create_table() methods to create a table and add fields to it. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$this->load->dbforge();

$fields = array(
        'id' => array(
                'type' => 'INT',
                'constraint' => 5,
                'unsigned' => TRUE,
                'auto_increment' => TRUE
        ),
        'name' => array(
                'type' => 'VARCHAR',
                'constraint' => '100',
        ),
        'email' => array(
                'type' => 'VARCHAR',
                'constraint' => '100',
                'null' => TRUE,
        ),
);

$this->dbforge->add_field($fields);
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('my_table');


Note that the $this->load->database() function must be called before you can use the database library.


I hope this helps! Let me know if you have any other questions.

by ryan.murray , a year ago

@herminia_bruen 

In CodeIgniter, you can create a table using the database functionalities provided by the framework. Here are the steps to create a table in CodeIgniter:

  1. First, make sure you have configured your database settings in the config/database.php file. Set the hostname, username, password, database, and dbdriver values according to your database configuration.
  2. Load the database library in your controller or model. You can do this by extending the CI_Controller in your controller file, or loading the database library in your model file using the $this->load->database(); method.
  3. After loading the database library, you can use the dbforge class to create tables. The dbforge class provides various methods to create tables, add fields, and set primary keys, indexes, etc. Example code to create a table: $this->load->dbforge(); $fields = array( 'id' => array( 'type' => 'INT', 'constraint' => 11, 'unsigned' => TRUE, 'auto_increment' => TRUE ), 'name' => array( 'type' => 'VARCHAR', 'constraint' => '100', ), 'email' => array( 'type' =>'VARCHAR', 'constraint' => '100', ), ); $this->dbforge->add_field($fields); $this->dbforge->add_key('id', TRUE); $this->dbforge->create_table('users'); In this example, we create a table called 'users' with three columns: 'id', 'name', and 'email'. The 'id' column is set as the primary key.
  4. Run the code to create the table. You can call this code in your controller, model, or a migration script.
  5. Once the code is executed, the table will be created in the database specified in your database configuration. You can check your database to verify the table creation.


Note: Before running the code to create the table, make sure the database connection is properly established by testing a simple query like $this->db->get('tablename'); in your controller or model.