2016-10-07 34 views
1

我在從數據庫中提取數據後對其中的某些數據進行排序,但似乎在執行查詢時出現某種類型的錯誤。我爲我的php框架使用codeignither。Mysql按錯誤順序排列CI

以下是錯誤

錯誤編號:1064

你在你的SQL語法錯誤;檢查對應於你的MySQL服務器版本在1號線

爲了使用近 'ORDER BY 'ban_created_at' ASC' BY 'ban_created_at' ASC

文件名正確的語法手冊:../application/models /Infractions_model.php

行號:33

而相應的腳本(Infractions_Model)

function fetch_infractions($filter = null, $direction = null){ 
     $query = "SELECT * FROM 'user_infractions', 'user' 
        WHERE 'user_infractions'.'user_id' = 'user'.'user_id' 
        AND 'user_infractions'.'ba_is_active' != '0'"; 
     if ($filter != null) { 
      if ($filter == 'age') { 
       $filter ='ban_created_at'; 
       switch ($direction) { 
        case 'ASC': 
         $dir = 'ASC'; 
         break; 
        case 'DESC': 
         $dir = 'DESC'; 
         break; 
        default: 
         $dir = 'ASC'; 
       } 
      } 
     } else { 
      $dir = 'ASC'; 
     } 
$query = "ORDER BY 'ban_created_at' " . $dir; 
/*Line 33*/ $result = $this->db->query($query, array($dir)); 

if ($result) { 
    return $result; 
}else{ 
    return false; 
} 
} 

回答

0

您的代碼:

$query = "ORDER BY 'ban_created_at' " . $dir; 
/*Line 33*/ $result = $this->db->query($query, array($dir)); 

嘗試波紋管糾正碼:初始化$query變量

$query .= " ORDER BY ban_created_at " . $dir; 
/*Line 33*/ $result = $this->db->query($query); 

第一次$query = ""; 但第二次相同的初始化$query變量$query = "";

所以,正確的是

$query = "query like this"; 
$query .= "order by like this"; 
+0

現在,我得到這個錯誤 錯誤編號正確的語法手冊:1064 你有一個錯誤在你的SQL語法中;檢查對應於你的MySQL服務器版本使用「」附近user_infractions',‘用戶’WHERE‘user_infractions’正確的語法手冊‘USER_ID’=‘用戶’。‘U’在行1 SELECT * FROM「 user_infractions','user'WHERE'user_infractions'。'user_id'='user'。'user_id'和'user_infractions'。'ba_is_active'!='0'ORDER BY ban_created_at ASC 文件名:../application/models/Infractions_model.php 行號:33 – David

+0

請避免在你的sql查詢(表名和字段名)單引號。 像'select * form'user''不正確。正確的是來自用戶的'seelct *' –

0

在錯誤

'ORDER BY 'ban_created_at' ASC' 
     ^   ^

做這樣

$query = "ORDER BY ban_created_at "."$dir"; 

而且你switch條件出來的IF條件將套房最佳

+0

錯誤仍然相同,但與上面的更改反映; 你的SQL語法有錯誤;檢查對應於你的MySQL服務器版本,在1號線附近使用「ORDER BY ban_created_at ASC」 ORDER BY ban_created_at ASC – David

0

看你的代碼,我想知道你是否在這個字段名稱ba_is_active上有拼寫錯誤,它笑是ban_is_active