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

[eluser]dgriffis[/eluser]
Codeigniter Database Error Handling

- I wanted to add error handling for my sql without confusing the user
with sql statements
- I ignored the standard error message configuration (index.php)
- I limited the error log to only display errors (log_threshold = 1)
- Then I turned database debugging to FALSE (config/database.php)
- Then I trapped the error with transaction processing below
(see DATABASE ERROR HANDLING below)
- The error information that was sent to the log file was a little
different (wrong number of fields versus actual SQL statement on screen)
but I accomplished my goal so I stopped here
- I'll turn database error handling back on in database.php
for now until I have all my other code changed and ready to roll out

Don't panic. Follow the information I have provided and you'll eventually
see my logic.



here are some notes that I took:

- LOGGING
where: /htdocs/application/<dir>/config/config.php
what: $config['log_threshold'] = 4; (set_error_handler set by this)
how: 0 no logging, 4 maximum loging

details: (log_threshold)
0 = Disables logging, Error logging TURNED OFF
1 = Error Messages (including PHP errors)
2 = Debug Messages
3 = Informational Messages
4 = All Messages

* I would recommend 1 for just errors

- DISPLAYING
where: /htdocs/index.php
what: error_reporting(E_ALL);
how: E_ALL display all, 0 display none

-- TRAPPING

A.
what: to show error and not log -
where: show_error('message')
how: change message to anything you want

B.
what: log messages
where: log_message('level', 'message')
how: change level to error/debug/info
details:
error = php errors
debug = debugging info
info = low priority messages

-- SUMMARY
to not show errors but log them
set DISPLAY config to error_reporting(0)
keep LOGGING $config['log_threshold'] to 1 (just errors) or above
trap errors with log_message('debug','any message')
show nice error message with show_error('nice message to contact support')


-- DATABASE ERROR HANDLING

// enable error reporting in config/database.php
// $db['default']['db_debug'] = FALSE;
// errors will be in /htdocs/system/logs/log-YYYY-MM-DD.php
// courtesy of www.ipersonnel.net

$this->db->trans_start();
$query = $this->db->query($sql);
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE)
{
log_message('debug', 'Problem Inserting Job');
show_error('Please notify support with transaction details');
return;
}
#2

[eluser]gtech[/eluser]
nice idea!

I turn db_debug to FALSE and do:

Code:
..
    $dbRet = $this->db->insert($table, $dataArray);
    if (!$dbRet) {
      $errNo   = $this->db->_error_number()
      $errMess = $this->db->_error_message();
      log_message("error", "Problem Inserting to ".$table.": ".$errMess." (".$errNo.")");
    }
    ..




Theme © iAndrew 2016 - Forum software by © MyBB