2016-04-20 69 views
-1

嗨由於某種原因,該工作我的搜索功能不起作用了我已經試過各種像/ or_like無濟於事的可能有人請點擊這裏伸出援助之手是我的控制器代碼:搜尋工作現在它不工作

 function contractor(){ 
      $keyword = $this->input->post('keyword'); 

      $this->db->like($keyword); 
      $this->db->like('contractor_location',$keyword); 
      $this->db->like('contractor_email',$keyword); 
      $this->db->like('contractor_description',$keyword); 
      $this->db->like('contractor_number',$keyword); 
      $this->db->like('contractor_website',$keyword); 
      $this->db->join('category','contractors.contractorID=category_name.catagory_id','inner'); 
      $this->db->group_by('contractor.contractorID'); 
      $query = $this->db->get('contractors'); 
      return $query->result(); 
    } 

UPDATE: 這是我收到遺憾的錯誤消息我忘了添加:

A Database Error Occurred 

Error Number: 1064 

You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax to use 
near 'LIKE '%%' ESCAPE '!' AND `contractor_location` LIKE '%%' ESCAPE 
'!' AND `contr' at line 4 
SELECT * FROM `contractors` INNER JOIN `category` ON 
`contractors`.`contractorID`=`category_name`.`catagory_id` WHERE LIKE 
'%%' ESCAPE '!' AND `contractor_location` LIKE '%%' ESCAPE '!' AND 
`contractor_email` LIKE '%%' ESCAPE '!' AND `contractor_description` 
LIKE '%%' ESCAPE '!' AND `contractor_number` LIKE '%%' ESCAPE '!' AND 
`contractor_website` LIKE '%%' ESCAPE '!' GROUP BY 
`contractor`.`contractorID` 

Filename: modules/search/models/Search_m.php 

Line Number: 38 
+0

什麼'不工作'是什麼意思?沒有結果?錯誤?你有沒有做過任何研究(var_dumps,DB上的手動查詢等)? – Pietro

+0

嗨對不起,我忘了添加錯誤,請參閱上面 – RiaanV

回答

2

您的代碼小姐列名

$this->db->like($keyword); 

而且你有錯誤的表連接

$this->db->join('category','contractors.contractorID=category_name.catagory_id','inner'); 

更正代碼:

function contractor(){ 
     $keyword = $this->input->post('keyword'); 
     $this->db->like('contractor_location',$keyword); 
     $this->db->like('contractor_email',$keyword); 
     $this->db->like('contractor_description',$keyword); 
     $this->db->like('contractor_number',$keyword); 
     $this->db->like('contractor_website',$keyword); 
     $this->db->join('category','contractors.contractorID=category.catagory_id','inner'); 
     $this->db->group_by('contractor.contractorID'); 
     $query = $this->db->get('contractors'); 
     return $query->result(); 
} 
+0

感謝您發現錯字!搜索正在工作,但我不得不改變一些你的答案,直到我得到了預期的結果。謝謝您的幫助 – RiaanV

1

它缺少列名

WHERE LIKE '%%' ESCAPE '!'

你需要糾正這一行$this->db->like($keyword);

0

最終工作代碼如下:

 function contractor(){ 
      $keyword = $this->input->post('keyword'); 
      $location = $this->input->post('location'); 
      $industry = $this->input->post('industry'); 

      $this->db->like('contractor_name',$keyword,$industry,$location); 
      $this->db->like('contractor_location',$keyword,$industry,$location); 
      $this->db->like('contractor_email',$keyword,$industry,$location); 
      $this->db->like('contractor_description',$keyword,$industry,$location); 
      $this->db->like('contractor_number',$keyword,$industry,$location); 
      $this->db->like('contractor_website',$keyword,$industry,$location); 
      $this->db->join('catagory','contractors.contractorID=catagory.ID','inner'); 
      $this->db->group_by('contractors.contractorID'); 
      $query = $this->db->get('contractors'); 
      return $query->result(); 
    }