简体   繁体   English

where子句中的CodeIgniter Active Record未知列

[英]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: 它一直给我错误在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();
}

在此处输入图片说明

Remove backticks and use AND in place of && 删除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();

Replace the backticks with single quotes . 单引号替换 引号 In MySql, backticks indicate that an indentifier is a column name: 在MySql中,反引号表示标识符是列名:

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

Replace below line 替换下面的行

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

with

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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