在codeigniter中执行SQL查询

时间:2016-09-22 20:40:25

标签: php mysql codeigniter

我需要执行我的SQL查询以在codeigniter脚本中复制MYSQL行。

这是我的代码

if ($para1 == 'duplicate'){
$query = $this->db->query("CREATE TEMPORARY TABLE tmptable_1 SELECT * FROM product WHERE `product_id` = ".$para2);
$query = $this->db->query("UPDATE tmptable_1 SET primarykey = NULL");
$query = $this->db->query("INSERT INTO product SELECT * FROM tmptable_1");
$query = $this->db->query("DROP TEMPORARY TABLE IF EXISTS tmptable_1");
}

但这不起作用。

1 个答案:

答案 0 :(得分:0)

您可以使用:

if ($para1 == 'duplicate'){
$table = 'product';
$primary_key_field = 'product_id';
$primary_key_val = $para2;
   /* generate the select query */
   $this->db->where($primary_key_field, $primary_key_val); 
   $query = $this->db->get($table);

    foreach ($query->result() as $row){   
       foreach($row as $key=>$val){        
          if($key != $primary_key_field){ 
          /* $this->db->set can be used instead of passing a data array directly to the insert or update functions */
          $this->db->set($key, $val);               
          }//endif              
       }//endforeach
    }//endforeach

    /* insert the new record into table*/
    return $this->db->insert($table); 
}