phpmysqlcodeigniterwildcard-expansion

CodeIgniter Wildcard Query


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');
        }

Solution

  • 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;