在Codeigniter中使用自定义查询时如何使用where

在Codeigniter中使用自定义查询时如何使用where,codeigniter,Codeigniter,我正在为我的数据库应用程序使用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"); 现在,如果我想使用wh

我正在为我的数据库应用程序使用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;
  }
我收到一个数据库错误,说“列值无效”

因为我的目的是根据我的筛选选项实现多个条件以生成结果。

尝试以下操作:

$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\u子句
变量:

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

谢谢@Yan,你节省了我的时间。你好@Yan,如果我可以在where_子句中使用多列,在where_值中使用多个条件,你能帮我一下吗?刚才我尝试使用数组,但它不起作用。如何使用order by与此SQL?将
order by
命令添加到
foreach
循环之后和
return
语句之前的
变量中。您可以在codeigniter文档中找到这方面的信息