Codeigniter to know if Insert worked?

Asked

Viewed 1,439 times

2

Using codeigniter how can I know if the Insert into worked really?

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

Via the variable $novo_usuario how can I be sure that the record worked?

1 answer

3


You can call the method affected_rows() which returns the number of lines 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
}

Refrencia

Database Quick Start

Queries

Browser other questions tagged

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