如何使用在Codeigniter中使用自定义查询的位置

时间:2012-06-17 10:10:21

标签: 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");

现在,如果我想使用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;
           }
    }

这只是一个例子,我必须为每个条件写出所有条件。所以,我可能会在codeigniter中遗漏一些东西,它可以实现这样的倍数,其中条件比我现在使用的更容易。

EDIT ::::

在@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”

因为我的目的是根据我的过滤器选项实现多个条件来生成结果。

1 个答案:

答案 0 :(得分: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;
}

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