我有这个有效的CI模型函数get_info
:
function get_info($item_id)
{
$this->db->from('items');
$this->db->where('item_id', $item_id);
$query = $this->db->get();
if($query->num_rows()==1)
{
return $query->row();
}
else
{
$item_obj=new stdClass();
$fields = $this->db->list_fields('items_tier_prices');
foreach ($fields as $field)
{
$item_obj->$field='';
}
return $item_obj;
}
}
我需要使用此自定义查询修改上述函数get_info
:
function get_info($item_id)
{
$sql_query = $this->db->query("select * from (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, items_tier_prices.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
from items
LEFT JOIN items_tier_prices
ON items.item_id=items_tier_prices.item_id
LEFT JOIN price_tiers
ON price_tiers.id=items_tier_prices.tier_id
where
price_tiers.name='jendela kaca mati single'
and items.deleted=0
union
select items.*
from items
left join items_tier_prices
ON items.item_id=items_tier_prices.item_id
where items_tier_prices.item_id is null
and items.deleted=0
) x
where x.item_id=".$item_id);
$query = $this->db->query($sql_query);
if($query->num_rows()==1)
{
return $query->row();
}
else
{
$item_obj=new stdClass();
$fields = $this->db->list_fields('items');
$fields = $this->db->list_fields('price_tiers');
$fields = $this->db->list_fields('items_tier_prices');
foreach ($fields as $field)
{
$item_obj->$field='';
}
return $item_obj;
}
}
问题是在修改查询和list_field之后,函数get_info
不再起作用。
我确定查询在phpmyadmin中有效,并且函数中生成的查询结果是SAME。它生成相同的列名称,只生成一行结果。
但我不确定:
$fields = $this->db->list_fields('items');
$fields = $this->db->list_fields('price_tiers');
$fields = $this->db->list_fields('items_tier_prices');
我的修改阻止函数get_info
工作有什么问题?
答案 0 :(得分:0)
您正在拨打$this->db->query
两次。
将编写查询的代码更改为:
$sql_query = "select * from (select items.name as name
..etc..";
然后在脚本中执行查询:
$query = $this->db->query($sql_query);
如果您的查询正确,则返回一行。请记住处理查询最终返回0行或多于1行的异常。
您还可以检查浏览器控制台。如果您的查询语法中存在错误,您会找到500 Internal Server Error
,它可以让您检查可能导致错误的原因。