简体   繁体   中英

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

在此处输入图片说明

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

Replace the backticks with single quotes . In MySql, backticks indicate that an indentifier is a column name:

$sql .= " && Name = '{$item}'"; 

Replace below line

if($item != 'All')
        $sql .= " && Name = `{$item}`"; 

with

if($item != 'All')
            $sql .= " && Name = '{$item}'"; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM