Codeigniter know if the insert worked?

2

Using codeigniter how can I know if the insert into actually worked?

$novo_usuario=$this->db->query("INSERT INTO usuario (nome, email) VALUES ('pessoa','[email protected]')");

Through the variable $novo_usuario how can I be sure that the registration has worked?

    
asked by anonymous 09.02.2015 / 20:15

1 answer

3

You can call the affected_rows () method that returns the number of rows affected by an INSERT / UPDATE / DELETE or check the return of query() is a boolean

$sql = "INSERT INTO usuario (nome, email) VALUES ('pessoa','[email protected]')";
if($this->db->query($sql)){
   echo 'Sucesso, linhas afetadas: '. $this->db->affected_rows();
}else{
  echo 'falha na operação';
  //rotina de log/tratamento de erro
}

Reference

Database Quick Start

Queries

    
09.02.2015 / 20:23