CodeIgniter Active Record where子句中的未知列

时间:2015-05-19 11:18:19

标签: php codeigniter activerecord

这是我的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();
}

enter image description here

3 个答案:

答案 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}'";