gpt4 book ai didi

php - 在同一 Controller 函数内执行不同查询的问题

转载 作者:行者123 更新时间:2023-11-29 17:49:15 24 4
gpt4 key购买 nike

这里我有函数index,它有两个不同的模型调用,如下所示:
Controller :

function index()
{
$people = $this->M_results->search_people(); // first model method call
// print_r($people); //shows result
$skills = $this->M_results->get_skill($category_name); // second model method call
}

型号:

function search_people()
{
$this->db->select("registration,gender,profile_img,location");
$this->db->from('search_result');
$this->db->join('services','search_result.registration = services.reg_id','left');
$this->db->join('review','search_result.registration = review.reg_id AND review.active = 0','left');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}

function get_skill($category_name)
{
$this->db->select('GROUP_CONCAT(skills.id ORDER BY skills.name ASC) as id,GROUP_CONCAT(skills.name ORDER BY skills.name ASC) as skill,sub_categories.name as sub_cat,class');
$this->db->from('skills');
$this->db->join('sub_categories','skills.sub_cat_id = sub_categories.id');
$this->db->join('categories','sub_categories.cat_id = categories.id');
$this->db->where('categories.name',$category_name);
$this->db->where('skills.active',0);
$this->db->group_by('sub_categories.name');
// echo $this->db->get_compiled_select(); exit();
$query = $this->db->get();
return $query->result();
}

现在的问题是,对 search_people 的调用返回了准确的结果。

但是在运行第二个模型调用(即 get_skill 函数)时,get_skill 的选择查询包含 search_result 中的选择查询列功能。并显示以下数据库错误:

Error Number: 1054

Unknown column 'registration' in 'field list'

SELECT registration, gender, profile_img, location,GROUP_CONCAT(skills.id ORDER BY skills.name ASC) as id, GROUP_CONCAT(skills.name ORDER BY skills.name ASC) as skill, sub_categories.name as sub_cat, class FROM skills JOIN sub_categories ON skills.sub_cat_id = sub_categories.id JOIN categories ON sub_categories.cat_id = categories.id WHERE categories.name = 'Child and Pet Care' AND skills.active =0 GROUP BY sub_categories.name

在上面的查询中,您可以看到 search_peopleget_skill 中的两列。

最佳答案

发布答案以供将来引用。

重置当前查询生成器状态。当您想要构建可以在某些条件下取消的查询时很有用。

$this->db->reset_query();

我从下面的链接找到了解决方案:
https://www.codeigniter.com/userguide3/database/query_builder.html#CI_DB_query_builder::reset_query

关于php - 在同一 Controller 函数内执行不同查询的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49529716/

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