Welcome Guest, Not a member yet? Register   Sign In
Is there a way to gracefully handle DB errors?
#1

[eluser]Jay Callicott[/eluser]
What irritates me ab CI (and I love CI I have 20+ apps running off a shared CI framework installation in production) - is that I don't see a way to gracefully handle DB errors, and in php programming most of the errors that can stop down apps are of course DB errors.

Most of us know about PEAR DB and MDB2 where you can run a query and say if PEAR:Big GrinB_Error($res) blah blah and you have a chance to log the SQL and or handle the error gracefully.

With CI if you have debugging on it shows a big DB error page with the error. I want a better way to handle DB errors. Trying a try/catch doesn't seem to work.

Anybody have ideas? It seems like the documention assumes you will never have any db errors and that's irritating. I wish I could run a query and have something like $this->db->db_error() or something where I could then handle the db error. Thoughts??
#2

[eluser]tonanbarbarian[/eluser]
after a quick look through the code you could try
$this->db->_error_message() or $this->db->_error_number

also $this->db->query() and $this->db->simple_query() etc all return false if there was an error
so something like this is a starting point
Code:
$query = $this->db->query($qry);
if ($query===false) {
  echo $this->db->_error_message();
}
#3

[eluser]Jay Callicott[/eluser]
Ya I would think that the best way to handle errors I suppose if you want to handle them gracefully would be to disable debugging for that database and then do something like:

Code:
$query = $this->db->query($qry);

if ($error = $this->db->_error_message()) {
//handle error gracefully
}
else {
//manipulate data
}

I think that will work. Thanks -J




Theme © iAndrew 2016 - Forum software by © MyBB