Py学习  »  DATABASE

Codeigniter MySQL根据会话中的用户ID过滤数据

MCIT Trends • 4 年前 • 211 次点击  

我的模型中有以下功能,可以根据会话中的用户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;
    }

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

Error Number: 42000/1064

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE `p_code` =' at line 2

SELECT * WHERE `p_code` =

Filename: C:/xampp/htdocs/doahrm/application/models/Officer_model.php

Line Number: 106

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

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/53666
 
211 次点击  
文章 [ 1 ]  |  最新文章 4 年前
TsaiKoga
Reply   •   1 楼
TsaiKoga    4 年前

根据原始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;
     }
}