2012-06-17 4 views
1

데이터베이스 응용 프로그램에 codeigniter를 사용하고 있습니다. codeigniter 데이터베이스 클래스에서 사용자 정의 쿼리에 get_where를 사용할 수 있는지 확실하지 않습니다. 내가Codeigniter에서 사용자 지정 쿼리를 사용할 때 어디에서 사용합니까?

function getVehicles($model='',$varient='') 
{ 
     if($model!='') 
      { 
      $q=$this->db->query("select model,varient,(select color from mtbl_colors where 
      mtbl_colors.colorid=mtbl_vehicles.colorid) as color from mtbl_vehicles 
      where model='$model'")->result(); 
      return $q; 
      } 
     elseif($varient!='') 
      { 
     $q=$this->db->query("select model,varient,(select color from mtbl_colors where 
      mtbl_colors.colorid=mtbl_vehicles.colorid) as color from mtbl_vehicles 
      where varient='$varient'")->result(); 
      return $q; 
      } 
    } 

, 나는 쿼리를 사용하고 where 절을 사용하여 위를 필터링 할 경우

이이 내가 있어야 할 곳에, 단지 예 지금 내 쿼리

$this->db->query("select model, varient, (select color from mtbl_colors where mtbl_colors.colorid=mtbl_vehicles.colorid) as color from mtbl_vehicles"); 

입니다 각 조건에 대한 모든 조건을 작성하십시오. 그래서, 나는 지금 사용하고있는 것보다 더 쉬운 여러 가지 조건을 달성 할 수있는 codeigniter에서 어떤 것을 놓칠 수 있습니다.

편집 ::::

내가 같은 시도 @Yan에서 제안이 내 의도가 있다는

"잘못된 열 값 Aarray"를 말하는 데이터베이스 오류가 발생

function getVehicles($where_clause='',$where_value='') { 
    $sql = "select model,varient,(select color from mtbl_colors where 
    mtbl_colors.colorid=mtbl_vehicles.colorid) as color from mtbl_vehicles"; 
    $where_clause=array("varient", "model", "colorid"); 
    $where_value=array("MAGNA 1.4", "SANTRO", "3") 

    if (!empty($where_clause) && !empty($where_value)) { 
     $sql .= " where $where_clause = ?"; 
    return $this->db->query($sql, $where_value)->result(); 
    } 
    return false; 
    } 

를 다음과 후 내 필터 옵션에 따라 결과를 생성하기 위해 여러 조건을 구현하십시오. 이 방법을 사용하여

$sql = "select model,varient,(select color from mtbl_colors where 
     mtbl_colors.colorid=mtbl_vehicles.colorid) as color from mtbl_vehicles"; 

$where_condition = ''; 
if (!empty($model)) { 
    $sql .= " where model = ?"; 
    $where_condition = $model; 
} 
elseif (!empty($varient)) { 
    $sql .= " where varient = ?"; 
    $where_condition = $varient; 
} 

if (!empty($where_condition)) { 
    return $this->db->query($sql, $where_condition)->result(); 
} 
return false; 

입력을 탈출 :

+0

당신은 CodeIgniter의 문서에서 이것에 대해 찾을 수 있습니다 [HTTP : //ellislab.com/codeigniter/user-guide/database/queries.html](http://ellislab.com/codeigniter/user-guide/database/queries.html) – machineaddict

답변

2

이 시도해보십시오.

편집 : 더 좋은 솔루션 있지만 where_clause 변수 살균해야합니다 -

function getVehicles($where_clause='',$where_value='') { 
    $sql = "select model,varient,(select color from mtbl_colors where 
     mtbl_colors.colorid=mtbl_vehicles.colorid) as color from mtbl_vehicles"; 

    if (!empty($where_clause) && !empty($where_value)) { 
     $sql .= " where $where_clause = ?"; 
     return $this->db->query($sql, $where_value)->result(); 
    } 
    return false; 
} 

편집 2를 배열 사용 :

function getVehicles($where_array) { 
    $sql = "select model,varient,(select color from mtbl_colors where 
     mtbl_colors.colorid=mtbl_vehicles.colorid) as color from mtbl_vehicles"; 

    $values = array(); 
    $counter = 0; 
    foreach ($where_array as $key => $value) { 
     if ($counter > 0) { 
      $sql .= " AND "; 
     } 

     $sql .= " where $key = ?"; 
     array_push($values, $value); 
     $counter ++; 
    } 
    return $this->db->query($sql, $values)->result(); 
} 
+0

감사합니다. @ 얀, 시간을 아끼 셨습니다. – Prajwal

+0

도움이 되었기 때문에 기쁩니다. –

+0

안녕하세요 @ 얀 where_value 내의 where_clause 및 여러 조건에서 여러 열을 사용할 수 있다면 도와 주실 수 있습니까? 그냥 배열을 사용해 보았지만 작동하지 않았습니다 – Prajwal

관련 문제