我在我的数据库应用程序中使用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 回答
试试这个:
使用这种方式逃避输入 .
编辑:一个更好的解决方案但你需要清理
where_clause
变量:编辑2 - 使用数组: