phpcodeigniterquery-builderquoting

CodeIgniter Active Record unknown column in where clause


This is my Active record query! It keeps giving me error that unknown column in where clause:

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


Solution

  • Remove backticks and use AND in place of &&

     $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();