Codeigniter IF在$ this-> db->里面选择

时间:2016-03-21 13:11:36

标签: codeigniter

我有codeigniter代码

$this->db->select("items.name as name, items.category as category, items.supplier_id as supplier_id, items.item_number as item_number,
 items.product_id as product_id, items.description as description, 
items.size as size, items.tax_included as tax_included, items.cost_price as cost_price, 
if(price_tiers.name='".$hasilsplit1."',items_tier_prices.unit_price,items.unit_price) as unit_price, items.promo_price as promo_price, 
items.start_date as start_date, items.end_date as end_date, items.reorder_level as reorder_level, items.item_id as item_id, items.allow_alt_description as allow_alt_description, items.is_serialized as is_serialized, items.image_id as image_id, items.override_default_tax as override_default_tax, items.is_service as is_service, items.deleted as deleted");
    $this->db->from('items');
    $this->db->join('items_tier_prices','items_tier_prices.item_id=items.item_id','left');
    $this->db->join("price_tiers","price_tiers.id=items_tier_prices.tier_id and price_tiers.name='".$hasilsplit1."'","left");
    $this->db->where('items.item_id', $hasilsplit0);
    $this->db->where('items.deleted', 0);

它会在部分上产生错误:

if(price_tiers.name='".$hasilsplit1."',items_tier_prices.unit_price,items.unit_price) as unit_price

错误是: 您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以获得正确的语法,作为unit_price使用,itemspromo_price作为promo_price,itemsstart_date as STA'在第1行

如何在$ this-> db-> select?

中正确编写IF语法

当我在var_dump时,显示

选择itemsname作为名称,itemscategory作为类别,itemssupplier_id作为supplier_id,{{1} }。items作为item_number,item_numberitems作为product_id,product_iditems作为说明,descriptionitems as尺寸,sizeitems为tax_included,tax_includeditems为cost_price,if(price_tiers.name =' Jendela Swing Double J106',{{ 1}}。cost_priceitems_tier_pricesunit_price为unit_price,itemsunit_price)为promo_price,itemspromo_price为start_date,itemsstart_date为end_date,itemsend_date为reorder_level,itemsreorder_level为item_id,itemsitem_id为allow_alt_description,itemsallow_alt_description为is_serialized,itemsis_serialized为image_id,itemsimage_id为override_default_tax, itemsoverride_default_tax as is_service,itemsis_service已删除FROM(items)LEFT JOIN deleted ON items。{ {1}} = items_tier_pricesitems_tier_prices LEFT JOIN item_id ON itemsitem_id = price_tiersprice_tiers和price_tiers。 name =' Jendela Swing Double J106' WHERE iditems_tier_prices =' 1' AND tier_iditems = 0

似乎块得到了字符` 感谢

1 个答案:

答案 0 :(得分:1)

我认为您需要查看手册,并尝试在compiling SELECT时禁用转义:

  

$this->db->select()接受可选的第二个参数。如果将其设置为FALSE,CodeIgniter将不会尝试保护您的字段或表名称。如果您需要复合选择语句,其中字段的自动转义可能会破坏它们,这非常有用。

第二个建议是在使用原始数据时转义SQL查询,例如:

$this->db->escape($someString); // instead of using $someString when concatenating string for query.