gpt4 book ai didi

php - 条件字符串中包含符号的 Codeigniter 查询

转载 作者:行者123 更新时间:2023-11-29 08:42:36 25 4
gpt4 key购买 nike

我正在尝试在我的 CMS 中添加过滤器下拉列表我的模型看起来像

public function load($sort,$order,$key,$value)
{ // $key='listening'; // $value="1";
//configure pagination
$config=array(
'base_url'=>base_url().'/index.php/companies/index',
'total_rows'=>$this->db->get('company')->num_rows(),
'per_page'=>$this->settings_model->get_per_page(),
'num_links'=>20
);

$this->pagination->initialize($config);



$this->db->select('company.id,
company.name,
company.logo,
company.status_id,
company.listening',FALSE);
$this->db->select('company_category.name as category,
company_category.id as category_id',FALSE);

$this->db->select('complain_status.cs_status as status',false);
$this->db->from('company');
$this->db->join('company_category','company_category.id = company.category_id');
$this->db->join('complain_stastus', 'complain_status.cs_id = company.status_id');



if(isset($_POST['key']))
{
$value= str_replace('&nbsp', ' ', $_POST['value']);
var_dump($value);
if($value!='0')
$this->db->having ($_POST['key'], mysql_real_escape_string($value) );

}
if($sort!='' || $sort!=NULL)
$this->db->order_by ($sort, $order);

$this->db->limit($config['per_page'], $this->uri->segment(3));

$result=$this->db->get();
if(!isset($_POST['key']))
$this->filter->set_filters_list($result->result_array());

return $result->result();
}

生成以下查询

SELECT company.id, company.name, company.logo, company.status_id, company.listening, company_category.name as category, company_category.id as category_id, complain_status.cs_status as status
FROM (`company`)
JOIN `company_category` ON `company_category`.`id` = `company`.`category_id`
JOIN `complain_status` ON `complain_status`.`cs_id` = `company`.`status_id`
HAVING `category` = 'Health & recreation'
LIMIT 20

正如你所看到的,当类别等于某个带有特殊字符的字符串(例如Health &creation)时,它会失败,即使我尝试了 CI 生成的查询,它也能在 MYSQL 上正常工作并让我明白结果

注意:我正在替换空格 $value= str_replace(' ', ' ', $_POST['value']); 因为此数据来自 select html 元素,该元素在选项中有空格,所以我必须稍后在后端代码中解析并删除它

提前致谢

最佳答案

代码点火器可能对 & 符号进行 html_encoding,以便它读取为 html 值。您可以通过将此行添加到运行查询的任何 Controller 或模型的构造函数来打开探查器来确认这一点:

$this->output->enable_profiler(TRUE);

如果我是对的,您的查询将替换为 & 之类的内容,而 & 应该是这样的。

请注意,分析器显示 &,而使用 $this->db->last_query() 仍显示 &

关于php - 条件字符串中包含符号的 Codeigniter 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13385731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com