简体   繁体   English

在CodeIgniter中验证count_all_results = 1

[英]Verify count_all_results = 1 in CodeIgniter

I'm trying to accomplish getting the current users session (if it exists) then comparing it to the database, along with a loggedIn value = 1 (meaning loggedIn) and count that a single row exists matching this where clause. 我试图完成获取当前用户会话(如果存在的话),然后比较它的数据库,用的loggedIn值一起= 1(指的loggedIn),并认为这是一种单一的行存在匹配此where子句。 If that exists, show logged in, else, show login form (shown in if{} else{} below) 如果存在,则显示已登录,否则,显示登录表单(显示在下面的if{} else{}中)

I have a model with a function: 我有一个带有功能的模型:

function check_if_loggedin(){
    #get user session id from db.
        $sess = $this->session->userdata('session_id');
    #set numerical value of successfully logged in equal 1
        $loggedInSetSuccess = 1;
    #compare session id to match in db (can only have only 1 match)
        $sessionsDbCompare = $this->db->get_where('Client',array('session_id'=>$sess,'loggedIn'=>$loggedInSetSuccess));

    if($sessionsDbCompare->count_all_results() = 1) {
    # User has valid session(valid sessID+loggedIn=0, show welcome
        echo 'Logged in baby';
        # show account pref   
    }
    else{
      # user doesnt have both a valid session and loggedIn is set to 0, show login form
      echo 'login form here';
    }  
}

The problem is - if($sessionsDbCompare->count_all_results() = 1) { my IDe is warning me, should I be using count_all_results() == 1 or count_all_results() === 1 . 问题是if($sessionsDbCompare->count_all_results() = 1) {我的IDe警告我,我应该使用count_all_results() == 1还是count_all_results() === 1 Sorry this is slightly newbie, I guess I'm a little confused because count_all_results() is a function. 抱歉,这有点新手,我想我有些困惑,因为count_all_results()是一个函数。 (I do understand comparison operators like "1" == "01") (我了解比较运算符,例如“ 1” ==“ 01”)

You should use == so the if statement would look like: 您应该使用==以便if语句如下所示:

if($sessionsDbCompare->count_all_results() == 1) { ... 

You can always use var_dump($sessionsDbCompare->count_all_results()); 您可以随时使用var_dump($sessionsDbCompare->count_all_results()); or similar to check the value returned... 或类似的检查返回的值...

You should be using this 你应该用这个

count_all_results() == 1

The way you have it is assigning 1 to what is left of it 您拥有的方式是 1 分配给剩余的内容

The IDE is also worried that count_all_results() might return false and 0. That's why it is suggesting the three === but dont worry about that. IDE还担心count_all_results()可能返回false和0。这就是为什么它建议使用三个===而不用担心的原因。 The two equals will fix it 两个等于将解决它

This line: 这行:

if($sessionsDbCompare->count_all_results() = 1) {

Should be: 应该:

if($sessionsDbCompare->count_all_results() == 1) {

您应该使用$ this-> db-> count_all()而不是$ this-> db-> count_all_results()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Codeigniter 随 `count_all_results` 改变 - Codeigniter changes with `count_all_results` Codeigniter count_all_results然后获取返回错误号1066 - Codeigniter count_all_results then get return error number 1066 CodeIgniter中带有Active Record的“count_all_results”和“where”的问题 - problem with “count_all_results” and “where” with Active Record in CodeIgniter 使用查询中的count_all_results - use count_all_results from query 正确使用count_all_results()? - Appropriate use of count_all_results()? CodeIgniter 中的 $query>num_rows() 和 $this->db->count_all_results() 之间的区别 & 推荐哪一个 - difference between $query>num_rows() and $this->db->count_all_results() in CodeIgniter & which one is recommended 如何在codeiginter中将count_all_results返回给ajax成功函数 - how to return count_all_results to ajax success function in codeiginter 使用 $this->db->count_all_results() 时出错; 作为对布尔成员函数 num_rows() 的调用 - Error for using $this->db->count_all_results(); as Call to a member function num_rows() on boolean 在查询中使用 count_all_results 或 get_compiled_select 和 $this->db->get('table') 列出表两次? - Using count_all_results or get_compiled_select and $this->db->get('table') lists table twice in query? 如何在Codeigniter中计算结果? - How to count results in Codeigniter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM