dongqian1893 2017-02-22 10:55
浏览 35
已采纳

CodeIgniter通配符查询

I am trying to perform a wildcard search query using CI.

I am checking for loggedin sessions. If a user is logged into more than one device, a popup is displayed. To check if the user is logged in from any other device, I perform a wildcard query to check the existence of its user id in the user_data column and return the number of rows. The problem I am facing is, even if I am logging in for the first time, it goes to the else clause and not to if or if-else clause.

In my controller, I am checking if it reaches the else clause, display a popup. However, it is going to the else clause even if someone is logging in for the first time.

Model:

public function update_bool($id) {
        $this->db->select('user_data');
        $this->db->from('ci_sess');
        $this->db->like('user_data', $id, 'both');
        $counter = $this->db->get()->row();
            if (empty($counter)) $counter = 0; 
            else if($counter === 1) $counter = 1;
            else $counter = 3;
        return $counter;
    }

Controller:

$counter = $this->ion_auth_model->loggedin_status_update_bool($userId);
        if($this->ion_auth_model->loggedin_status_update_bool($userId) === 3) {
          warning('multiple_session_title', 'multiple_session_text');
        }
  • 写回答

2条回答 默认 最新

  • dougai2427 2017-02-22 11:04
    关注

    You need to count number of rows return by the query. And based on no of records, your condition will work. Currently its returning one row of type array. And $counter as array to match else if condition will always fail.

    I hope this will help you.

    .... 
    $query = $this->db->get();
    $counter = $query->num_rows();
    if (empty($counter)) {
      $counter = 0; 
    }else if($counter === 1) {
       $counter = 1;
    } else {
     $counter = 3;
    }
    return $counter;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包