Welcome Guest, Not a member yet? Register   Sign In
Dealing with db errors
#1

[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();
$result = $query->result();
if ($result) {
//do something
return $result;
}
return false;

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
#2

[eluser]LuckyFella73[/eluser]
I do it this way:

1. place your sql code in a model, for example:
Code:
<?php
class your_model extends Model
{
    function your_model()
    {
        parent::Model();
    }
    
    function your_function()
    {
        $query = $this->db->get(); // whatever you want to do here
        if ( $query )
        {
            return $query;
        }
        else
        {
            return false;
        }
    }
}
?>

2. call the sql code/ model from controller like:
Code:
$query = $this->your_model->your_function();
if ( $query === FALSE )
{
    // here come what you want tell the user about the failure
    // for example:
    $data['error_msg'] = "something went wrong";
}

I wouln'd print out the query itself (only while debugging) for
security reasons.
#3

[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='')
{
    if ($str)
    {
        log_message('error', 'my_model ->' . $str);
        $this->error_str = $str;
    }
}
#4

[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() {
    if($something) {
        return true
    }
    $this->_error('There was a problem');
    return False; ????
}

How do i then access the $this->error_str in my controller to display the message to the user??

Thanks in advance,

Dan
#5

[eluser]Yorick Peterse[/eluser]
You can also use the show_error() function, e.g:

Code:
<?php

//Function to retrieve the comments
    function get_comments() {
        //Create the query
        $query = $this->db->get('comments');
        //Check to see if there are any comments at all
        if($query->num_rows() > 0) {
            //Return the results
            return $query->result();
        }
        //No comments
        else {
            show_error('No comments have been added yet');
        }
        
    }
?>
#6

[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.




Theme © iAndrew 2016 - Forum software by © MyBB