【问题标题】:Codeigniter MySQL filtering data as per user ID in the sessionCodeigniter MySQL 根据会话中的用户 ID 过滤数据
【发布时间】:2020-01-26 03:37:57
【问题描述】:

我的模型中有以下功能,可以根据会话中的用户 ID 选择记录。

public function getOfficer()
    {
     $usr = $this->session->userdata('id_user');
     $userArray = $this->db->order_by('last_name','ASC')->where_in('tbl_officer.p_code', [8,10,24]);
     $userArray1 = $this->db->order_by('last_name','ASC')->get_where('tbl_officer', array('status' => 1, 'usr'=>$this->session->userdata('id_user')));

     if($usr == 4){
        $this->db->where('p_code',$userArray );
     }else{
        $this->db->where('usr',$userArray1);
     }
     $q = $this->db->get('tbl_officer');
     if ($q->num_rows() > 0) {
        return $q->result();
     }
     return false;
    }

如果会话中的用户 4,记录应按 p_code [8,10,24] 过滤,会话中的任何其他用户,记录应按 usr 过滤。 usr 列包括用户 ID,如 1、2、3、4 等。

但函数出现以下错误,并没有得到预期的结果。

错误号:42000/1064

您的 SQL 语法有错误;检查手册 对应于您的 MariaDB 服务器版本,以便使用正确的语法 在第 2 行的“WHERE p_code =”附近

选择 * WHERE p_code =

文件名:C:/xampp/htdocs/doahrm/application/models/Officer_model.php

行号:106

函数中的第 106 行是 $q = $this->db->get();

可能出了什么问题?有人可以帮忙吗?

【问题讨论】:

    标签: php mysql sql codeigniter


    【解决方案1】:

    根据原始 sql,from table_name 丢失了:

    SELECT * WHERE `p_code` = ...
    

    所以你失去了你的桌子:

    $q = $this->db->get('table_name');
    

    而且我认为您的代码需要如下所示:

    public function getOfficer()
    {
         $usr = $this->session->userdata('id_user');
         if ($usr == 4) {
             $query = $this->db->order_by('last_name','ASC')->where_in('tbl_officer.p_code', [8,10,24]);
         } else {
             $query = $this->db->order_by('last_name','ASC')
                               ->where(array('status' => 1, 'usr'=>$usr));
         }
         $query = $query->get('tbl_officer');
         if ($query->num_rows() > 0) {
             return $query->result();
         } else {
             return false;
         }
    }
    

    【讨论】:

    • @TsaiKoga。修改为我的编辑。但仍然有同样的问题。
    • @TsaiKoga。那是错误号:42000/1064 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 3 行的 '' 附近使用正确的语法 SELECT * FROM tbl_officer WHERE p_code = 文件名:C:/xampp/htdocs/doahrm/application/models/Officer_model。 php行号:107
    • @TsaiKoga。选择 * 从 tbl_officer 哪里 p_code =
    • @MCITTrends 现在它可以找到你的桌子了。但是Officer_model.php 出现新错误 Line Number: 107,这行代码是什么?
    • @TsaiKoga。 $q = $this->db->get('tbl_officer');
    猜你喜欢
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 2015-10-27
    • 1970-01-01
    • 2019-04-29
    相关资源
    最近更新 更多