活动记录codeigniter转换问题

时间:2016-02-29 23:59:50

标签: php codeigniter

我正在尝试将下面的查询翻译成codeigniter,但似乎没有运气在我身边

     public function activation ($email, $token) 
    {

       $search =  $this->db->query("SELECT email, token, active FROM gamers WHERE email='".$email."' AND token='".$token."' AND active='0'"); 
       $match  = mysql_num_rows($search);

       if($match > 0){
           // We have a match, activate the account
           mysql_query("UPDATE gamers SET active='1' WHERE email='".$email."' AND token='".$token."' AND active='0'") or die(mysql_error());
           redirect('index.php/email_verification/account_activated');
       }else{
            // No match -> invalid url or account has already been activated.
            redirect('index.php/email_verification/activation_error');
          }

       }

     }

我试过这个

public function activation ($email, $token)  {


          $search =  $this->db->query("SELECT email, token, active FROM gamers WHERE email='".$email."' AND token='".$token."' AND active='0'"); 
          $match  = mysql_num_rows($search);
          $query = $this->db->get();
          $match = $query->num_rows();


         if($match > 0){
        // We have a match, activate the account
            mysql_query("UPDATE gamers SET active='1' WHERE email='".$email."' AND token='".$token."' AND active='0'") or die(mysql_error());
            redirect('index.php/email_verification/account_activated');
        }else{
            // No match -> invalid url or account has already been activated.
             redirect('index.php/email_verification/activation_error');
              }

          }

    }

但不会工作。

1 个答案:

答案 0 :(得分:1)

使用有效记录:

$this->db->select('mail, token, active');
$this->db->where('email',$email);
$this->db->where('token',$token);
$this->db->where('active', 0);
$query = $this->db->get('gamers'); //table name here 

if($query->num_rows() > 0){
   //do something
   //to update
   $this->db->set('active',1); //You must set in order to update
   $this->db->where('email', $email);
   $this->db->where('token',$token);
   $this->db->where('active', 0);
   if($this->db->update('gamers'){
       redirect('index.php/email_verification/account_activated');
    }else{
       //handle it son
    }
}