7

I'm working on an api, it handles the requests which comes from clients, then gets the response from server(developed using codeigniter 3) and forwards that back to client.

But, in case of any database errors, like duplicate id, or null values, the model class cannot handle that error to display a proper error message. I've tried the try catch block but not succeeded yet. Here's the model:

public function add() {
    try {
        $this->db->trans_start(FALSE);
        $this->db->insert('users', $preparedData);
        $this->db->trans_complete();
        if ($this->db->trans_status() === FALSE) {
            throw new Exception("Database error:");
            return false;
        }
        return TRUE;
    } catch (Exception $e) {
        log_message('error: ',$e->getMessage());
        return;
    }
}

One thing to mention, I've set db_debug to FALSE.

Any help would be appreciated.

5
  • Before inserting please check the value in database Commented Jan 18, 2017 at 10:03
  • I tried checking, but still it is not displaying error. Commented Jan 18, 2017 at 10:41
  • How are you validating it? Can you please update the question with check code before insert? Commented Jan 18, 2017 at 10:49
  • 1
    You can refer this for solution: stackoverflow.com/questions/44732546/… Commented Jun 27, 2017 at 5:37
  • 1
    you can check solution here : stackoverflow.com/questions/15858372/… Commented Aug 9, 2017 at 6:13

2 Answers 2

11

As for CI 3, below code gets database error code and error message. db_debug is set to FALSE.

public function add() {
    try {
        $this->db->trans_start(FALSE);
        $this->db->insert('users', $preparedData);
        $this->db->trans_complete();

        // documentation at
        // https://www.codeigniter.com/userguide3/database/queries.html#handling-errors
        // says; "the error() method will return an array containing its code and message"
        $db_error = $this->db->error();
        if (!empty($db_error)) {
            throw new Exception('Database error! Error Code [' . $db_error['code'] . '] Error: ' . $db_error['message']);
            return false; // unreachable retrun statement !!!
        }
        return TRUE;
    } catch (Exception $e) {
        // this will not catch DB related errors. But it will include them, because this is more general. 
        log_message('error: ',$e->getMessage());
        return;
    }
}

Refer to documentation at https://www.codeigniter.com/userguide3/database/queries.html#handling-errors

saying

If you need to get the last error that has occurred, the error() method will return an array containing its code and message.

It is a bit incomplete in my opinion because it does not show error code and error message in the example code.

Sign up to request clarification or add additional context in comments.

2 Comments

Using CI 3.1.5, $this->db->error() always return a non empty array, thus I had to test if(! $db_error['code']) to make this solution work.
immediately after each statement you have to check the result of the statement and keep error in a variable and later if trans result is false use that error variable, please check my solution
0

I just lost an hour trying to figure out why I can't get the error in my code. You have to check for an error after each statement! Working solution:

  function insertUpdate($data) {

    $order = $data->order;
    $order_products = $data->order_products;

    $this->db->trans_start();

    $order->user_id = $this->session->user_id;

    $error = "OK";

    if (!$this->db->insert('_order', $order)) {
      $error = $this->db->error()["message"];
    }

    $id = $this->db->insert_id();

    foreach ($order_products as $row) {
      $row->order_id = $id;
      if (!$this->db->insert('_order_product', $row)) {
        $error = $this->db->error()["message"];
        break;
      }
    }

    $order_code = substr(md5($id), 0, 6);

    if (!$this->db->where('order_id', $id)) {
      $error = $this->db->error()["message"];
    }

    if (!$this->db->update('_order', ["order_code" => $order_code])) {
      $error = $this->db->error()["message"];
    }

    $this->db->trans_complete();

    return [
        'result' => $error, 'order_code' => $order_code
    ];

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.