1

I am trying catch database errors within a transaction and if one occurs then rollback and throw an exception.

However, the code is stopping and displaying the db error screen before it throws the exception.

Any ideas how I can make it detect db error without stopping running the subsequent code?

try {
    $this->my_function($data);
} catch (Exception $e) {
    var_dump($e);
}

private function my_function($data)
{

    $this->db->trans_start();
    foreach($data as $reg)
    {
        $sql = $this->db->insert_string('my_table', $reg);
        if($this->db->query($sql))
        {
            continue;
        } else {
            $this->db->trans_rollback();
            throw new Exception('Exception message here...');
        }
    }
    $this->db->trans_complete();

}
7
  • what is the error displaying on screen before it throws the exception.? Commented Oct 11, 2016 at 10:30
  • I've purposely mis-spelt a column name to create an error Commented Oct 11, 2016 at 10:31
  • it is expected behavior. error display immediately when it occurs. if condition execute after that Commented Oct 11, 2016 at 10:34
  • So is there any way to produce the results I want? Commented Oct 11, 2016 at 10:35
  • you should handle exception in order to control error messages.but codeigniter provide better way to do this see trans_status() codeigniter.com/user_guide/database/transactions.html.but still error will display if you don't handle it Commented Oct 11, 2016 at 10:37

1 Answer 1

1

This has been answered before on this question

As answered by cwallenpoole:

In application/config/database.php set

// suppress error output to the screen
$db['default']['db_debug'] = FALSE;

In your model or controller:

// try the select.
$dbRet = $this->db->select($table, $dataArray);
// select has had some problem.
if( !$dbRet )
{
   $errNo   = $this->db->_error_number()
   $errMess = $this->db->_error_message();
   // Do something with the error message or just show_404();
}

Or in you case:

private function my_function($data)
{
    $errors = array();
    $this->db->trans_start();
    foreach($data as $reg)
    {
        $sql = $this->db->insert_string('my_table', $reg);
        if($this->db->query($sql))
        {
            continue;
        } else {
            $errNo   = $this->db->_error_number()
            $errMess = $this->db->_error_message();
            array_push($errors, array($errNo, $errMess));
        }
    }
    $this->db->trans_complete();
    // use $errors...
}   

Even better

I believe this question has all the answers you need because it takes multiple inserts into account and let's you finish the once that did not return an error.

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

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.