Welcome Guest, Not a member yet? Register   Sign In
Handling Database Errors
#1

[eluser]IanMcQ[/eluser]
Hi there,

I'm having some trouble figuring out the best way to handle database errors with CodeIgniter. Upon submission of one of my forms, I need to check for a database error, so I've gone like this:

Code:
$query = $this->db->query([...]);

// Form validation
if ($this->db->_error_number() == [...])
{
    // display error
}

The problem is, the built-in CodeIgniter "Database Error" page takes over, which doesn't really allow me to handle the error on the form. I've considered turning db_debug off, but that would create another problem for me. I really like having db_debug on, because I've created a nice page where my users can physically submit in a bug report with the respective error. It's been very helpful in solving database issues.

So, my question is: how do I effectively handle MySQL database errors on my form when I want to, but leave the unhandled errors to the stock CI "Database Error" page?

Any thoughts? Thanks in advanced.
#2

[eluser]yacman[/eluser]
In your /application/config/database.php file
Code:
//Never show database errors
$config['db_debug'] = FALSE;
...

To do this one off, leave default as TRUE, and in places you want db debug off.

Code:
$this->db->db_debug = false;
$this->db->query();
...

You'll need to flip it back on after turning it off if that same db instance is utilized later in that thread and you want db debug on.

Generally, just either have it off or on.

Also, you can use transactions, and check the status of the transaction.





Theme © iAndrew 2016 - Forum software by © MyBB