2016-12-13 1 views
2

내 codeigniter 앱에서 문제가 발견되었습니다. 나는 4 개의 다른 like와 or_like를 사용하여 간단한 검색 쿼리를 작성했다.Codeigniter like, or_like가 작동하지 않습니다.

$q = $this->db->select('*') 
->from('table') 
->like('col', $search_string1, both) 
->or_like('col', $search_string2, both) 
->or_like('col', $search_string3, both) 
->or_like('col', $search_string4, both) 
->get()->results 

그리고 내가 원하는,하지만 난 "활성"라는 내 테이블에 새 COL을 추가하기로 결정하고 내가 활성 가지고 행만을 표시 할대로 작동 = 1. 그래서 난 내 쿼리에 어디에 추가하려고 ,하지만 예상대로 작동하지 않았습니다.

$q = $this->db->select('*') 
->from('table') 
->where('active', 1) 
->like('col', $search_string1, both) 
->or_like('col', $search_string2, both) 
->or_like('col', $search_string3, both) 
->or_like('col', $search_string4, both) 
->get()->results 

이 쿼리는 활성 세트가 0으로 설정된 행을 표시합니다. 대답은 CI에서 SQL을 배우는 사람들에게 명백하지 않을 수 있기 때문에

+0

상태가 다음과 같아야합니다. – Gulshan

답변

1

안녕하세요 당신은 코드를

$q = $this->db->select('*') 
->from('table') 
->where('active', 1) 
->where("(col LIKE '%".$search_string1."%' OR col LIKE '%".$search_string2."%' OR col LIKE '%".$search_string3."%' OR col LIKE '%".$search_string4."%')", NULL, FALSE) 
->get()->results; 

감사 @의 Roshan의 코드에 대한 설명은

+0

예, 고마워요. – SakuragiRokurota

2

을 다음 사용하시기 바랍니다 수 있습니다. (나는 또한 코드에서 오류가 수정되었습니다.)

$q = $this->db 
       // ->select('*') // you don't need select('*') if you want everything 
       // ->from('table') // Don't need this either 
       ->where('active', 1) 
       ->where("(col LIKE '%".$search_string1."%' OR col LIKE '%".$search_string2."%' OR col LIKE '%".$search_string3."%' OR col LIKE '%".$search_string4."%')", NULL, FALSE) 
       ->get('table') // add your table name here 
       ->result(); // it's not "->results" 

그래서 최종 쿼리가 다음과 같을 것이다 :

"get all results from 'table' 
    where active = 1 AND `col` = search_string1 
OR where active = 1 AND `col` = search_string2 
OR where active = 1 AND `col` = search_string3 
OR where active = 1 AND `col` = search_string4 

Then assign the result object to $q 

간단한 방법 :

여기
$q = $this->db->where('active', 1) 
       ->where("(col LIKE '%".$search_string1."%' OR col LIKE '%".$search_string2."%' OR col LIKE '%".$search_string3."%' OR col LIKE '%".$search_string4."%')", NULL, FALSE) 
       ->get('table') 
       ->result(); 

무엇 위 요구하고있어 그것을 볼 수 있지만 코드를 작성하는 더 복잡한 방법 (이해하기 쉽게) :

$q = $this->db 
      // where active=1 AND col LIKE %$search_string1% 
      ->where('active', 1) 
      ->like('col', $search_string1, 'both') 

      // OR where active=1 AND col LIKE %$search_string2% 
      ->or_where('active', 1) 
      ->like('col', $search_string2, 'both') 

      // OR where active=1 AND col LIKE %$search_string3% 
      ->or_where('active', 1) 
      ->like('col', $search_string3, 'both') 

      // OR where active=1 AND col LIKE %$search_string4% 
      ->or_where('active', 1) 
      ->like('col', $search_string4, 'both') 

      ->get('table') 
      ->result(); 

이렇게해야 할 때가 있습니다. 예를 들어 active이 여러 주일 수 있습니다. on, off, pending이며 특정 상태에 대한 해당 입력란을 확인해야합니다. 귀하의 경우에는 필요하지 않으므로 active은 항상 1이지만, active 일 수 있다면 둘 이상의 질문 빌더를 사용해야합니다.

+0

멋지게 대답했습니다. 따옴표는' 'both''이어야합니다. +1 –

+0

@AkshayHegde를 붙잡아서 고마워요. –

-1

당신의 쿼리 후 라인 아래 추가

는 에코 $ this-> DB-> last_query(); 죽을;

해고 된 쿼리가 표시되고 어디에서 잘못 되었습니까? 여전히 전체 쿼리를 복사하여 MySQL 콘솔에 붙여 넣는 것 이상의 결과를 얻지 못한다면 오류가 발생합니다.

관련 문제