【问题标题】:database query in codeigniter where_in is arraycodeigniter 中的数据库查询 where_in 是数组
【发布时间】:2020-10-22 09:28:02
【问题描述】:

我有一些 id 存储在数据库表中,列名为“defaultdest”,如“1,2,3,4,5,6”。 我已经在控制器中获取了它,现在我需要在另一个名为“destinations”的表中进行查询。我需要根据 Ids 获取目的地的地方是“1,2,3,4,5,6”。

这是另一个表的查询结果

public function index()
    {
        $data = $this->data;
        $idlists = explode(",", get_option('defaultdest'));
        $data['destlist'] = $this->ui_model->destfetch($idlists);
        $this->load->view('themes/ui/home/'.get_option('uihometype').'', $data);
    }

这是我在模型中的数据库查询

function destfetch($idlists)
         {
            $this->db->set_dbprefix('');
            $this->db->select('*');
            $this->db->where_in('id', $idlists);
            $this->db->from("destinations");
            $query = $this->db->get();

            //print_r($this->db->last_query());
            return $result = $query->result_array();
            
         } 

注意:idlists 值是(由 get_option 方法返回)

$idlists = "1,2,3,4,5,6";

这不起作用,得到空白结果。

【问题讨论】:

  • 您是否检查过打印您在代码print_r($this->db->last_query()); 中评论过的查询,您在查询中得到了什么,您可以向我们展示并添加问题..

标签: php codeigniter codeigniter-3


【解决方案1】:

Where_in 适用于数组而不是字符串。所以你的 $idlists 不应该是一个带有 '1,2,3,4,5' 的字符串,而是一个 id 数组。

你可以这样做:

$idlists = "1,2,3,4,5,6";            //String
$this->db->where_in('id', explode(',', $idlists));

注意:- explode() 用于将字符串拆分为数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-03
    • 2016-10-13
    • 2017-03-22
    • 1970-01-01
    • 2022-06-23
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多