Codeigniter会话中的多个查询结果

时间:2017-02-13 22:24:52

标签: php codeigniter session

我是编程的先行者。我想在SQL中检查我的int的值并将其保存到变量。这是一个结果,这不是问题。但是当它有两个结果时我遇到了问题。对不起我的混乱英语。我希望你理解我

    $this->db->where('login', $login);
    $this->db->where('password', $password);
    $result = $this->db->get('users');
    $this->db->where('admin', 1);     // I want to get users who have admin as "1"
    $admin = $this->db->get('users'); // Admin result ???

    if($result->num_rows() > 0)
    {

        $user = array(
            'username'  => md5($login),
            'logged_in' => true,
            // Now i don't know how to check admin var is equal to 1. If 1 i want to add something like this:
            'admin' => true
        );

        $this->session->set_userdata($user);
        return true;
    }

1 个答案:

答案 0 :(得分:0)

您无需进行2次查询即可检查用户是否为管理员。只需检查所选行的结果:

    $this->db->where('login', $login);
    $this->db->where('password', $password);
    $query = $this->db->get('users');

    $row = $query->row();

    if (isset($row)) {
      $user = array(
        'username'  => md5($login),
        'logged_in' => true,
        'admin' => $row->admin //IF YOU HAVE 1 = ADMIN IN YOUR TABLE?
      );

      $this->session->set_userdata($user);
      return true;
    }

如果您需要将1/0转换为true / false,则替代:

 if (isset($row)) {
  $admin = ($row->admin == 1) ? true : false; // IF DB HAS INTEGER 1
  $user = array(
    'username'  => md5($login),
    'logged_in' => true,
    'admin' => $admin
  );

  $this->session->set_userdata($user);
  return true;
}