• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Handling errors from the database (key violation)

Hello. I'm a newbie in CI and I'm trying to create my first application. Check the error below, it's occurs because a primary key in my database has duplicated:

An Error Was Encountered
Error Number: 1062
Duplicate entry '57-1' for key 1
INSERT INTO user_has_movie SET id_movie=57, id_user=1

How I can catch this Database error and throws an exception? Thanks.

P.S. Sorry if my english is not good.

$dbRet = $this->db->insert($table, $data_array);
    if (!$dbRet) {
      $ret['ErrorMessage'] = $this->db->_error_message();
      $ret['ErrorNumber'] = $this->db->_error_number();
      log_message('error', "DB Error: (".$ret['ErrorNumber'].") ".$ret['ErrorMessage']);
    } else {

this is how I catch db errors in CI.

I can't catch the error. The execution stops in this line:

$dbRet = $this->db->insert($table, $data_array);

This function don't throws any Exception? That way I can use a try-catch to handle the error. Remember, the SQL query is correct, but can sometimes makes a key violation in the primary key of the table (Duplicate key entry).

Thanks for help.

Hi! You can try catch the error in CI error template in /system/application/errors/error_db.php. I think this is not a good way, but i still not found more correct solution. Also by this way you can catch other errors. I was trying to change this templates to generate correct JSON for catch errors in AJAX application, but this is not works good Sad

the reason the execution stops in your case is because db_debug is on.. you need to set db_debug to FALSE in the database config file, to catch db errors like my example above. (which is tested on 1.6.2 with MYSQL).

sounds good, but in this case you are needed make checks for errors after each db query. it would be nice to have some feature for handle errors like hooks

yeah I have created wrapper code to do this for the db functions I use. so rather than call $this->db I call $this->mydb which then calls $this->db.

I might post up this code when I modify it for general use.

Thanks guys. With the db_debug set to FALSE I can get the exception.

Hello everybody,

[quote author="jeffersonrpn" date="1213335600"]Thanks guys. With the db_debug set to FALSE I can get the exception.[/quote]

I have a similar issue. I've set the db_debug set to FALSE, and the error message is displayed no more, but I cannot get the exception. The try-catch block is the following:

$str_insert = $this->db->insert_string('customers',$data);
catch(Exception $e){
    $data['msg'] = $e;

The catch block doesn't run
Any clue?

When a query fails the db code will NOT throw an execption. the call $this->db->query will return true if it succeeds and null if the query fails.

$data = array('name' => 'John', 'email' => '[email protected]');
$str = $this->db->insert_string('customers', $data);

// returns null if SQL error occurs.
$res = $this->db->query($str);

if (!$res) {
  // if query returns null
  $msg = $this->db->_error_message();
  $num = $this->db->_error_number();

  $data['msg'] = "Error(".$num.") ".$msg;

Ensure DB_DEBUG is set to FALSE in the database config file, or execution will halt when a mysql error occurs (it does not get thrown, it justs exits from the php interpreter)

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.