php - Adding column if not exists in sql Opencart -
i creating model in open cart. using vqmod writing code in model. want alter table adding coloumn if not exists in table. here code.
$field_name = 'geozoneid'; $col = $this->db->query("select ".$field_name." ".db_prefix."order"); if (!$col){ $this->db->query("alter table ".db_prefix."order add ".$field_name." int(11) first"); $zone_id=array(); $zone_id=$this->db->query("select zgz.geo_zone_id `oc_zone_to_geo_zone` zgz zone_id = (select shipping_zone_id oc_order order_id = '".$order_id."' ) "); $zone_id=$zone_id->row['geo_zone_id']; $query=$this->db->query("update `oc_order` set geozoneid ='".$zone_id."' order_id ='".$order_id."'"); } else { $zone_id=array(); $zone_id=$this->db->query("select zgz.geo_zone_id `oc_zone_to_geo_zone` zgz zone_id = (select shipping_zone_id oc_order order_id = '".$order_id."' ) "); $zone_id=$zone_id->row['geo_zone_id']; $query=$this->db->query("update `oc_order` set geozoneid ='".$zone_id."' order_id ='".$order_id."'"); }
i dont know why not executing not alter tabel.
i ended fixing problem this..
$hasmodelchoicefield = false; $result = $this->db->query( "describe `".db_prefix."order`;" ); foreach ($result->rows $row) { if ($row['field'] == 'geozoneid') { $hasmodelchoicefield = true; break; } } if (!$hasmodelchoicefield) { $sql = "alter table `".db_prefix."order` add geozoneid int( 11 ) character set utf8 collate utf8_general_ci default ''"; $this->db->query( $sql ); $zone_id=array(); $zone_id=$this->db->query("select zgz.geo_zone_id `" . db_prefix . "oc_zone_to_geo_zone` zgz zone_id = ( select shipping_zone_id `".db_prefix."order`where order_id = '".$order_id."' ) ");$zone_id=$zone_id>row['geo_zone_id'];$query=$this->db->query("update `" . db_prefix . "order` set geozoneid ='".$zone_id."' order_id ='".$order_id."'"); } else{ $zone_id=array(); $zone_id=$this->db->query("select zgz.geo_zone_id from`oc_zone_to_geo_zone` zgzwhere zone_id = ( select shipping_zone_idfrom oc_orderwhere order_id = '".$order_id."' ) ");$zone_id=$zone_id->row['geo_zone_id'];$query=$this->db->query("update `" . db_prefix . "order` set geozoneid ='".$zone_id."' order_id ='".$order_id."'");
}
Comments
Post a Comment