Check if data already exists in the bank with codeigniter

2

I am starting with codeigniter using the active record to make the conversation with the bank, and wanted to know how do I know if an email already exists in db, would it list all emails and create a foreache doing the verification? Is there a simpler way?

    
asked by anonymous 19.12.2015 / 20:13

2 answers

1

Try this:

$this->load->database();
$query = $this->db->get_where('tabela',array('colunaemail'=>'email'));
$result = $query->result_array();
if(count($result) > 0) { //Existem } else { //não existe }

See the IC manual:

link

    
19.12.2015 / 22:25
1

You can do with the form_validation library. You pass the field name that should be unique and the table name.

is_unique will validate this.

is_unique[tbUsuario.campoEmail]

Stay the same

$this->form_validation->set_rules('inputEmail', 'Email', 'required|valid_email|is_unique[tbUsuario.campoEmail]');

The inputEmail should be replaced with the name of your input in the HTML, then the name of the table and the name of the field in the database. Fit according to your model.

Then perform the validation:

if(!$this->form_validation->run())
   ... fluxo de erro aqui  
    
03.11.2016 / 22:54