0

Could really use some help. I'm using CodeIgniter 3.0 and I'm trying to design it so database errors will be caught in a try catch block, and in the catch block i can get a detailed description of the database error, then code will continue execution after the catch block.

The best I have managed to do is throw errors, but I cant get a description of the message so its pretty useless.

In index.php, i have set:

define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');

in config.php, i have

$config['log_threshold'] = 4;
$config['log_path'] = '';
$config['log_file_extension'] = '';
$config['log_file_permissions'] = 0644;
$config['log_date_format'] = 'Y-m-d H:i:s';
$config['error_views_path'] = '';

in database.php, i have

$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
    'dsn'   => '',
    'hostname' => $hostname,
    'username' => $username,
    'password' => $password,
    'database' => $database,
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => FALSE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
   'save_queries' => TRUE
);

Then i have the actual model where some code executes via ajax. It is basically this

try {

    $batch = array();
    for ( $x = 0; $x < count($selectedIds); $x++ ) {
        $data = array(
            "picklist_id" => $picklistId,
            "item_index" => $selectedIds[$x],
            "text" => $selectedTexts[$x]
        );
        $batch[] = $data;
    }

    if( !$this->db->insert_batch("p_picklists_created_selecteds", $batch) ) 
    {
        throw new Exception('DB error');
    }

} catch (Exception $ex) {

    $this->M_debug->log("ERROR!!!!");
    $this->M_debug->log($this->db->error()['message']);
    // output the error
    $this->M_debug->logError($ex);

}   

The database error that I'm trying to catch is there is actually no table called p_picklists_created_selecteds, so the insert_batch query fails.

Now i have managed to get this to work in a sense as i mentioned. The check to see if the query is false causes the exception to be thrown. But the call to my custom M_debug model to log to console the result of $this->db->error()['message']); just results in an empty string. I only know what the actual error was from manually examining the code, in the future i need to know the actual messages.

Furthmore, i'm not sure all CI database failed queries will return false (do they?) it would be nice if there's a db error of some kind i could check for it, something along the lines of

$this->db->insert_batch("p_picklists_created_selecteds", $batch)
if ( $this->db->error()['code'] != 0 ) {
    // throw error
}

instead of

if( !$this->db->insert_batch("p_picklists_created_selecteds", $batch) ) 
{
    throw new Exception('DB error');
}

But this has no effect.

also, you might think that setting 'db_debug' => TRUE in database.php might help, but if i do that then i can get some sort of database error message in chrome console which is nice, but then the above code doesnt throw an error, in other words the code in the exception block doesnt execute, and the code doesnt continue execution everything just stops at the error in an ajax request

Any help would be appreciated. Thanks

Geoff L
  • 765
  • 5
  • 22
  • So there are multiple methods `$this->db->error()` might be useful to you – Alex Oct 20 '18 at 23:30
  • Possible duplicate of [CodeIgniter - how to catch DB errors?](https://stackoverflow.com/questions/7843406/codeigniter-how-to-catch-db-errors) – Alex Oct 21 '18 at 00:22
  • How can you expect to have data integrity if you continue executing queries after one fails? Also, a try-catch block catches PHP errors, not necessarily MySQL errors. I use a solution to log MySQL errors in my production environments, and then I use a cron to email me this log every 10 minutes. If this would be an acceptable answer to your question, I can share that with you. – Brian Gottier Oct 21 '18 at 05:37
  • hey @BrianGottier I'd love to check it out just to compare with what I am doing (which isn't much in that regard compared to CI's built in logger). Can you pop it on pastebin? – Alex Oct 24 '18 at 06:11

0 Answers0