1

データベース アプリケーションに codeigniter を使用しています。codeigniter データベース クラスのカスタム クエリに get_where を使用できるかどうかはわかりません。

これは私のクエリです

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

where句を使用して上記をフィルタリングしたい場合は、クエリを次のように使用しています

  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 条件ごとにすべての条件を記述する必要があります。そのため、現在使用しているよりも簡単に複数の条件を達成できる codeigniter の何かが欠けている可能性があります。

編集::::

@Yanからの提案の後、次のように試しました

 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;
  }

「無効な列値 Aarray」というデータベース エラーを受け取りました

私の意図は、フィルターオプションに従って結果を生成するために複数の条件を実装することでした。

4

1 に答える 1

2

これを試して:

$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;

この方法を使用すると、入力がエスケープされます。

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;
}

EDIT 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();
}
于 2012-06-17T11:01:39.220 に答える