How do you log database query errors?

11 views
Skip to first unread message

Peter Karunyu

unread,
Jan 30, 2012, 1:27:29 AM1/30/12
to codeigni...@googlegroups.com, Isaak Mogetutu
Below is my typical function in a model. Each function, upon failed execution of a query, submits a log entry, but I think the code can be made better. Other than wrapping the entire bold part in another function, how else can I do it?

function getSingleDocuments($id)
    {
        $sql = "SELECT id, `name`FROM documents WHERE id=$id";
 
        $result = $this->db->query($sql);
        if ($result)
        {
           return $result;
        }
        else {
            $result['ErrorMessage'] = $this->db->_error_message();
              $result['ErrorNumber'] = $this->db->_error_number();
              $path = $this->uri->uri_string();
              log_message('error', "DB Error: (".$result['ErrorNumber'].") ".$result['ErrorMessage']. ' Path: '.$path);
              show_error('<p>A database error has occurred, a notification about this error has been sent to tech support.</p>');
            return FALSE;

        }
    }

--
Regards,
Peter Karunyu
-------------------

Reply all
Reply to author
Forward
0 new messages