Php 删除codeigniter中的单引号
我正在开发Php 删除codeigniter中的单引号,php,mysql,codeigniter,Php,Mysql,Codeigniter,我正在开发搜索功能。 我创建了一个搜索表单,用户可以根据类型,操作&格式搜索应用程序 我在我的join查询中使用了子查询,以获得所需的结果。 我已经在MySQL Workbench中测试了我的查询,它运行良好 但当我使用查询生成器技术在Codeigniter中尝试相同的查询时,我遇到了一个问题 以下是在workbench中运行良好的查询: SELECT (*) FROM `App` LEFT JOIN `App_type` ON `App_type`.`app_id` = `App`.`id`
搜索
功能。
我创建了一个搜索表单,用户可以根据类型
,操作
&格式
搜索应用程序
我在我的join查询中使用了子查询,以获得所需的结果。
我已经在MySQL Workbench中测试了我的查询,它运行良好
但当我使用查询生成器技术在Codeigniter中尝试相同的查询时,我遇到了一个问题
以下是在workbench中运行良好的查询:
SELECT (*)
FROM `App`
LEFT JOIN `App_type`
ON `App_type`.`app_id` = `App`.`id`
LEFT JOIN `App_formate`
ON `App_formate`.`app_id` = `App`.`id`
WHERE `App`.`id` IN(select app_id FROM App_type WHERE type_id in (3,2,6) group by app_id HAVING COUNT(*) = 3)
AND `App_formate`.`formate_id` IN('1', '3')
AND `jobs`.`ope_min` <= '3'
AND `jobs`.`ope_max` >= '3'
GROUP BY `jobs`.`id`;
此子查询中的单引号正在产生问题
到目前为止,我所尝试的:
为了删除这句话,我已经试过了
REPLACE($subquery,'')
->其中_in('App.id',trim($subquery,“'))
$subquery\u improved=substr($subquery,1,-1)代码>
注意:我知道
$this->db->query()
,但不想使用它。我认为您的$subquery
被视为参数字符串,而不是查询。活动记录方法的通用语法为
->where_in('field_name', array() || 'string values');
您$subquery
必须像这样
$subquery = $this->db
->select('app_id')
->from('App_type')
->where_in('type_id',array(3,2,6))
->group_by('app_id')
->having(' COUNT(*) = 3')
->get()
->row()
->app_id;
希望它能起作用:)我的方法是下面这样的,以一种更通用的方式。我总是试图遵循MVC模式,同时将功能分解为小功能。虽然你没有分享你所有的代码,我还是会建议你 您应该将我的自定义给定名称更改为函数、数组等,以便它与您的代码匹配。希望能有帮助 模型函数 模型函数 而不是
SELECT (*)
使用
而不是
WHERE `App`.`id` IN(
select app_id FROM App_type
WHERE type_id in (3,2,6) group by app_id HAVING COUNT(*) = 3)
->where_in('App.id',$subquery) //<-- Here is the problem
使用
(翻译成CodeIgniter留给读者作为练习。)使用手动查询(不安全,使用非常小心)
替换此代码:
->where_in('App.id',$subquery) //<-- Here is the problem
->where_in("App.id", $subquery, FALSE)
第三个参数accept Boolean确定函数是否应转义值和标识符。当设置为FALSE时,它不使用单引号作为转义字符
希望这有帮助。首先,在查询之后,输入以下代码:
->where_in('App.id',$subquery) //<-- Here is the problem
->where_in("App.id", $subquery, FALSE)
echo$this->db->last_query();退出
这将打印查询,您将知道问题在哪里。还要复制该查询并尝试在phpmyadmin中运行它
还可以尝试这种类型的解决方案,因为您有单报价问题:
在子查询中,使用单引号写入$type\u count,即“$type\u count”
->where("App.ope_min <='",$data['ope_value']."'")
->where("App.ope_max >='",$data['ope_value']."'")
->group_by("App.id", "desc")
->get();
->其中(“App.ope_min=”,$data['ope_value'..”)
->分组人(“应用程序id”、“说明”)
->get();
如果您想询问其他问题,请告诉我。只需在where_in语句中添加第三个参数。试试这个-
$subquery = "select app_id FROM app_type WHERE type_id in (3,2,6) group by app_id HAVING COUNT(*) = 3";
$search_app_query = $this->db
->select('*')
->from('app')
->join('app_type', 'app_type.app_id = app.id', 'left outer')
->join('app_formate', 'app_formate.app_id = app.id', 'left outer')
->where_in('app.id',$subquery, FALSE)
->where_in('app_formate.formate_id',array(1,3))
->where('app.ope_min <=',3)
->where('app.ope_max >=',3)
->group_by("app.id", "desc")
->get()->result();
$subquery=“从app\u类型中选择app\u id,其中(3,2,6)组中的type\u id由计数(*)为3的app\u id组成”;
$search\u app\u query=$this->db
->选择(“*”)
->来自('应用程序')
->加入('app\u type','app\u type.app\u id=app.id','left outer')
->加入('app\u formate','app\u formate.app\u id=app.id','left outer')
->where_in('app.id',$subquery,FALSE)
->其中_in('app_format.format_id',数组(1,3))
->其中('app.ope_min=',3)
->分组人(“应用程序id”、“说明”)
->get()->result();
//如果子查询中没有更改,则首先确认子查询只返回一个结果
$subquery = "select GROUP_CONCAT(CONCAT(\"'\",app_id,\"'\")) FROM App_type WHERE type_id in ($selected_type) group by app_id HAVING COUNT(*) = $type_count";
$search_app_query = $this->db
->select('*')
->from('App')
->join('App_type', 'App_type.app_id = App.id', 'left outer')
->join('App_formate', 'App_formate.app_id = App.id', 'left outer')
->where_in('App.id',$subquery,false) //<-- pass 3rd parameter as false for removing single quotes
->where_in('App_formate.formate_id',$data['selected_formates'])
->where('App.ope_min <=',$data['ope_value'])
->where('App.ope_max >=',$data['ope_value'])
->group_by("App.id", "desc")
->get();
$subquery=“从app\u类型中选择GROUP\u CONCAT(CONCAT(\“'\”,app\u id,\“'\”),其中,$type\u id在($selected\u type)组中,按具有COUNT(*)=$type\u COUNT的app\u id分组”;
$search\u app\u query=$this->db
->选择(“*”)
->来自('应用程序')
->加入('App\u type','App\u type.App\u id=App.id','left outer')
->加入('App\u formate','App\u formate.App\u id=App.id','left outer')
->where_in('App.id',$subquery,false)//where_in('App_format.format_id',$data['selected_formates']))
->其中('App.ope_min=',$data['ope_value'])
->分组人(“应用程序id”、“说明”)
->get();
我建议您不要将子查询直接置于where in条件中,因为有时您可能也会得到零结果。所以最好的方法是像这样单独执行子查询
$subdata = $this->db->select("app_id ")->from("App_type ")->where_in("type_id",$selected_type)->group_by(""app_id)->having("COUNT(*) = $type_count")->get()->result_array();
$search_app_query = $this->db
->select('*')
->from('App')
->join('App_type', 'App_type.app_id = App.id', 'left outer')
->join('App_formate', 'App_formate.app_id = App.id', 'left outer');
if(!empty($subdata)) {
$this->db->where_in('App.id',$subdata);
}
$this->db->where_in('App_formate.formate_id',$data['selected_formates'])
->where('App.ope_min <=',$data['ope_value'])
->where('App.ope_max >=',$data['ope_value'])
->group_by("App.id", "desc")
->get();
$subdata=$this->db->select(“app_id”)->from(“app_type”)->where_in(“type_id”),$selected_type)->group_by(“app_id”)->having(“COUNT(*)=$type_COUNT”)->get()->result_array();
$search\u app\u query=$this->db
->选择(“*”)
->来自('应用程序')
->加入('App\u type','App\u type.App\u id=App.id','left outer')
->加入('App_formate','App_formate.App_id=App.id','left outer');
如果(!empty($subdata)){
$this->db->where_in('App.id',$subdata);
}
$this->db->where_in('App_formate.formate_id',$data['selected_formates']))
->其中('App.ope_min=',$data['ope_value'])
->分组人(“应用程序id”、“说明”)
->get();
谢谢大家的建议
我已经找到了在不改变其他代码的情况下解决问题的方法
多亏了他的评论才是解决这个问题的关键。
解决方案就是改变我之前想要的一行代码
$search_app_query = $this->db
->select('*')
->from('App')
->join('App_type', 'App_type.app_id = App.id', 'left outer')
->join('App_formate', 'App_formate.app_id = App.id', 'left outer')
// This is what he suggested me to change and it works.
->where('App.id IN('.$subquery.')')
->where_in('App_formate.formate_id',$data['selected_formates'])
->where('App.ope_min <=',$data['ope_value'])
->where('App.ope_max >=',$data['ope_value'])
->group_by("App.id", "desc")
->get();
$search\u app\u query=$this->db
->选择(“*”)
->来自('应用程序')
->加入('App\u type','App\u type.App\u id=App.id','left outer')
->加入('App\u formate','App\u formate.App\u id=App.id','left outer')
//这就是他建议我改变的,而且它是有效的。
->其中(“.$subquery.”中的“App.id”)
->其中_in('App_formate.format_id',$data['selected_formates'))
->其中('App.ope_min=',$data['ope_value'])
->分组人(“应用程序id”、“说明”)
->get();
再次感谢您。您的任务看起来非常简单 而不是
WHERE `App`.`id` IN(
select app_id FROM App_type
WHERE type_id in (3,2,6) group by app_id HAVING COUNT(*) = 3)
->where_in('App.id',$subquery) //<-- Here is the problem
您可以在Codeigniter文档(第4点和下面的部分)中找到这一确切信息
我不熟悉这个框架——特别是如何将该子查询作为实际的子查询传入。但是,如果文档没有明确说明,并且子查询的结果不是令人难以接受的,只需自行执行并将结果传入:
where_in('App.i
$subquery = "select GROUP_CONCAT(CONCAT(\"'\",app_id,\"'\")) FROM App_type WHERE type_id in ($selected_type) group by app_id HAVING COUNT(*) = $type_count";
$search_app_query = $this->db
->select('*')
->from('App')
->join('App_type', 'App_type.app_id = App.id', 'left outer')
->join('App_formate', 'App_formate.app_id = App.id', 'left outer')
->where_in('App.id',$subquery,false) //<-- pass 3rd parameter as false for removing single quotes
->where_in('App_formate.formate_id',$data['selected_formates'])
->where('App.ope_min <=',$data['ope_value'])
->where('App.ope_max >=',$data['ope_value'])
->group_by("App.id", "desc")
->get();
$subdata = $this->db->select("app_id ")->from("App_type ")->where_in("type_id",$selected_type)->group_by(""app_id)->having("COUNT(*) = $type_count")->get()->result_array();
$search_app_query = $this->db
->select('*')
->from('App')
->join('App_type', 'App_type.app_id = App.id', 'left outer')
->join('App_formate', 'App_formate.app_id = App.id', 'left outer');
if(!empty($subdata)) {
$this->db->where_in('App.id',$subdata);
}
$this->db->where_in('App_formate.formate_id',$data['selected_formates'])
->where('App.ope_min <=',$data['ope_value'])
->where('App.ope_max >=',$data['ope_value'])
->group_by("App.id", "desc")
->get();
$search_app_query = $this->db
->select('*')
->from('App')
->join('App_type', 'App_type.app_id = App.id', 'left outer')
->join('App_formate', 'App_formate.app_id = App.id', 'left outer')
// This is what he suggested me to change and it works.
->where('App.id IN('.$subquery.')')
->where_in('App_formate.formate_id',$data['selected_formates'])
->where('App.ope_min <=',$data['ope_value'])
->where('App.ope_max >=',$data['ope_value'])
->group_by("App.id", "desc")
->get();
->where_in('App.id',$subquery) //<-- Here is the problem
->where("App.id IN (".$subquery.")",NULL, false)