首页 文章

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

提问于
浏览
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;
           }
    }

这只是一个例子,我必须为每个条件写出所有条件 . 所以,我可能会在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”

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

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

相关问题