Louis Milotte
Louis Milotte

Reputation: 59

Codeigniter clone table with active record or dbforge

Is there a way to implement:

CREATE TABLE sample LIKE master;    

using either Codeigniter active record or dbforge?

The reason being is that this is being executed from an administration panel; the admin is given a form and the new table name is captured from a POST.

Upvotes: 2

Views: 4558

Answers (1)

vijaykumar
vijaykumar

Reputation: 4806

You can try this

$tablename = $_POST['tblname'];
$mastertable = $_POST['mastertable']
$this->db->query("CREATE TABLE $tablename LIKE $mastertable");

To copy data

$query = $this->db->get($mastertable);
foreach ($query->result() as $row) {
      $this->db->insert($tablename,$row);
}

Upvotes: 4

Related Questions