Check if email already exists with codeigniter

Asked

Viewed 472 times

1

I want to check if an email already exists in the database. I am doing the following.

$this->load->database();
      $query = $this->db->get_where('usuario', array('email'));
      $result = $query->result_array();
      if(count($result) > 0) {
        echo "email existe";

      } else { echo "email não existe";}

Only that always returns me that the email already exists even if I type a different one. Where is my mistake? Or I am doing wrong and there is another way?

  • Use the Codeigniter validation system. Use validation is_unique.

1 answer

2


I thought of the following solution:

$this->db->select('email');
$this->db->where('email', $this->input->post('email'));
$retorno = $this->db->get('usuario')->num_rows();

if($retorno > 0 ){
    echo "este e-mail já está cadastrado";
} else { 
    echo "este e-mail está disponível";
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.