Dealing with db errors - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Dealing with db errors (/showthread.php?tid=18249) |
Dealing with db errors - El Forum - 04-30-2009 [eluser]september28[/eluser] Hi there, Was just wondering how people deal with db errors in their apps. at the moment i code using the following methodology: Code: $query = $this->db->get(); So basically the next step will only happen as long as the db query went ok. but what if it didnt? then the function would just return false which isn't very useful. I was just wondering how people deal with db errors - I myself would like to inform the user that a db error has occured and perhaps log the exact details somewhere for me to sift through and work out why the error occurred. Is there any standardized way of doing this that i can apply to all my functions in the same way? Does anyone have any other ideas? Cheers, Dan Dealing with db errors - El Forum - 04-30-2009 [eluser]LuckyFella73[/eluser] I do it this way: 1. place your sql code in a model, for example: Code: <?php 2. call the sql code/ model from controller like: Code: $query = $this->your_model->your_function(); I wouln'd print out the query itself (only while debugging) for security reasons. Dealing with db errors - El Forum - 04-30-2009 [eluser]TheFuzzy0ne[/eluser] I like to do both. I return FALSE, and set an public $error_str variable in the model. If logging is enabled, any error is also logged. Code: function _error($str='') Dealing with db errors - El Forum - 05-03-2009 [eluser]september28[/eluser] thanks guys for the suggestions. TheFuzzyOne: So you call the _error function from another function in the model, and then do you return the $this->error_str in the other function? so: Code: function test() { How do i then access the $this->error_str in my controller to display the message to the user?? Thanks in advance, Dan Dealing with db errors - El Forum - 05-03-2009 [eluser]Yorick Peterse[/eluser] You can also use the show_error() function, e.g: Code: <?php Dealing with db errors - El Forum - 05-03-2009 [eluser]TheFuzzy0ne[/eluser] Yes, that's exactly what I'd do. _show_error() logs the error and sets the error_str. You're app should return FALSE, giving you're application a chance to recover. |