0

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');
        }
Ankur Sinha
  • 6,473
  • 7
  • 42
  • 73

2 Answers2

1

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;
Naincy
  • 2,953
  • 1
  • 12
  • 21
0

I think your shorthand might be off a little and if I understand what you mean then there would be multiple sessions for the same user in the table meaning you couldn't get it using ->row().

Try like this:

public function update_bool($id) {
    $this->db->select('user_data');
    $this->db->from('ci_sess');
    $this->db->like('user_data', $id, 'both');
    $query = $this->db->get();

    return $query->num_rows(); // This will give you now many times this user exists in the database, eg 0, 1, 2, 3
}

Then clean up your controller so you're not calling the database twice:

$counter = $this->ion_auth_model->loggedin_status_update_bool($userId);
if($counter == 3) {
      warning('multiple_session_title', 'multiple_session_text');
}
Solvision
  • 193
  • 1
  • 11