【问题标题】:CodeIgniter model unable to return specific recordsCodeIgniter 模型无法返回特定记录
【发布时间】:2016-03-07 01:37:19
【问题描述】:

我正在使用 CodeIgniter,但在使用 results() 方法从表中获取所有行时,我无法让 where() 选择方法工作。

这是我的模型:

public function get_all_entries($id)
    {
        // Select row to be fetched
        $this->db->where('id', $id);
        $this->db->get('users');
        // Execute the find query with provided data
        $query = $this->db->get('users');
        // Return an object with all the data
        return $query->result();
    }

它应该返回与users 表中的$id 参数匹配的所有行,但它只是获取表中的所有记录,包括那些与提供的$id 不匹配的记录论据。

我在这里做错了什么?我尝试了row() 方法,虽然它与where() 一起使用,但它只返回一行,因此不适合我的情况。

【问题讨论】:

    标签: php mysql codeigniter model rows


    【解决方案1】:

    问题是你调用了 get() 方法两次,第一次使用 where,但没有分配给变量;第二个被分配给一个变量,但由于 where 子句已经被另一个使用,它得到了一切。删除第一个 get 就可以了。

    public function get_all_entries($id)
    {
        // Select row to be fetched
        $this->db->where('id', $id);
        // Execute the find query with provided data
        $query = $this->db->get('users');
        // Return an object with all the data
        return $query->result();
    }
    

    【讨论】:

    • 我试过了,但是还有另一个问题(如果 id 为零或不在表中)让我认为这不是解决方案,现在我解决了这个问题,你提到的解决方案工作,谢谢。
    • 能否给这个问题投票?真的很感激。
    猜你喜欢
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    • 2019-02-07
    • 1970-01-01
    相关资源
    最近更新 更多