dimanche 26 juillet 2015

PHP If statement returns false on MySQL boolean column return

I am using the CodeIgniter framework. I'm working on the user authentication portion of my website. In my 'users' table of my MySQL database I have a column that I declared as a BOOLEAN (I am aware that it really is a TINYINT(1)) called 'verified' that denotes whether or not a user's email address has been verified. When I try to test the value of this column, my IF statement always evaluates to FALSE, even though I know for a fact the value is 1. Here is a snippet of my code:

public function authenticate_user(){
    $pw = hash('sha512', $this->salt1 . $this->input->post('password') . $this->salt2);
    $email = $this->input->post('email');

    $query = $this->db->get_where('users', ['email' => $email, 'password' => $pw]);
    if($query->num_rows() == 1){
        $row = $query->row_array();
        //die("verified = ".$row['verified']); <-This line shows 'verified = 1' consistently when uncommented.
        if($row['verified'] == 1){
            $this->session->set_userdata([
            'name' => $row['name'],
            'email' => $email,
            'login_time' => time(),
            'last_activity' => time(),
            'session_valid' => TRUE
            ]);
            return true;
        }
        else return 'unverified';
    }
    else return false;
}

When the credentials are correct, this function always returns 'unverified' and no session variables are set. The function exhibits the correct behavior when the credentials are incorrect. I have confirmed in PHPMyAdmin that the column is 1, and I have confirmed that 1 is returned from the database by uncommenting the die() statement above. I have tried using true and '1' (string) in place of the integer 1 and have gotten the same result. Am I doing something wrong? Does CodeIgniter preprocess database returns in a way that would make this not work?

Thank you in advance,

Eric

Aucun commentaire:

Enregistrer un commentaire