这是我的Active记录查询!它不断给我一个错误,即where子句中的未知列:
public function getItemSale($start, $end = 9,$userid,$loc_id,$item)
{
$sql = "SELECT Name, sum(Qty) as total, sum(Price) as sale from itransfile where
SESSIONID = GETSESSIONID(?) && PayMode IN('CASH','Cheque')" ;
if(is_numeric($loc_id))
$sql .= " && location_id = " .$loc_id ;
else
$sql .= " && location_id IN(SELECT location_id FROM client_locations where client_id = " .$userid. ")";
if($item != 'All')
$sql .= " && Name = `{$item}`";
$sql .= " group by Name order by sale desc LIMIT ? OFFSET ?;";
$query = $this->db->query($sql, array(date("Y-m-d"),$end,(int)$start));
return $query->result_array();
}
答案 0 :(得分:1)
删除backticks
并使用AND
代替&&
$sql = "SELECT Name, sum(Qty) as total, sum(Price) as sale from itransfile where
SESSIONID = GETSESSIONID(?) AND PayMode IN('CASH','Cheque')";
if (is_numeric($loc_id))
$sql .= " AND location_id = " . $loc_id;
else
$sql .= " AND location_id IN(SELECT location_id FROM client_locations where client_id = " . $userid . ")";
if ($item != 'All')
$sql .= " AND Name = '{$item}'";
$sql .= " group by Name order by sale desc LIMIT ? OFFSET ?;";
$query = $this->db->query($sql, array(date("Y-m-d"), $end, (int) $start));
return $query->result_array();
答案 1 :(得分:0)
将反引号替换为单引号。在MySql中,反引号表示标识符是列名:
$sql .= " && Name = '{$item}'";
答案 2 :(得分:0)
替换下面的行
if($item != 'All')
$sql .= " && Name = `{$item}`";
与
if($item != 'All')
$sql .= " && Name = '{$item}'";