Question

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.

Was it helpful?

Solution

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top