The solution to this problem is to get sqlstate and verify that it is 1062
(Duplicate entry). Use the method error()
in case of failure it returns an array with the code and the description of the error .
Your code should be something like:
$msg = '';
if(!$this->db->query('INSERT....')){
$error = $this->db->error();
if($error['code'] == 1062){
$msg = 'Registro duplicado';
}
}
In order for CI to not display this giant error message you can disable it via the configuration file application / config / database.php by changing the following setting to false
.
Before / Original:
'db_debug' => (ENVIRONMENT !== 'production')
Then:
'db_debug' => false
Also possible to do this in specific snippets via code, just change the db_debug
$msg = '';
$this->db->db_debug = false;
if(!$this->db->query('INSERT....')){
$error = $this->db->error();
if($error['code'] == 1062){
$msg = 'Registro duplicado';
}
}
$this->db->db_debug = true;