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

编辑 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 回答