簡體   English   中英

where子句中的CodeIgniter Active Record未知列

[英]CodeIgniter Active Record 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();
}

在此處輸入圖片說明

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

單引號替換 引號 在MySql中,反引號表示標識符是列名:

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

替換下面的行

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM