Welcome Guest, Not a member yet? Register   Sign In
Transaction and error handling
#1

[eluser]ekstro[/eluser]
Code:
if ($this->db->trans_status() === FALSE)
{
    // generate an error... or use the log_message() function to log your error
}

How I can get error number and msg with transaction? I can get queries list, queries execute time... but i don't see errors info.
#2

[eluser]InsiteFX[/eluser]
Managing Errors

If you have error reporting enabled in your config/database.php file you'll see a standard error message if the commit was unsuccessful. If debugging is turned off, you can manage your own errors like this:
Code:
$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->trans_complete();

if ($this->db->trans_status() === FALSE)
{
     // generate an error... or use the log_message() function to log your error
}
If it returns FALSE then you know that this is the database query that caused the Error!

InsiteFX
#3

[eluser]ekstro[/eluser]
Yes, but how I can get error info (number and message).
#4

[eluser]InsiteFX[/eluser]
You can not unless you use pure MySql commands!

you can set your own Error numbers when you show the error for that query!

InsiteFX
#5

[eluser]ekstro[/eluser]
In $this->db I have all transaction queries, queries execute times and lot of other infos ... but i can't check error msg from mysql ... (i of coz use CI AR).
#6

[eluser]InsiteFX[/eluser]
I also use nothing but CI AR!

You will need to build your own Error system give me a few minutes and I'll put something together to get you started!

InsiteFX
#7

[eluser]InsiteFX[/eluser]
Would be something like this.
Code:
/**
* You can place this in a helper or in your code.
*
* USAGE: $error = $this->show_trans_error('1');
*/
public function show_trans_error($status_code = '')
{
    $trans_errors = array(
        'Transaction Successful!',
        'Error 01: Transaction Insert Failed!',
        'Error 02: Transaction Update Failed!'
    );

    return $trans_errors[(int)$status_code];
}

You could add to the application/config/config.php file a switch for logging etc.
Code:
// this will allow you to either display the error message or log it to the log file.
$config['trans_logging'] = TRUE;

The check:
Code:
$trans_logging = FALSE;    // This is better then using the above $config but your choice!
$error = $this->show_trans_error('0');

if ($this->db->trans_status() === FALSE)
{
    if ($this->trans_logging === FALSE)
    {
        // display however you would like $error.
    }
    else
    {
        // save $error to log file!
        $this->log_message('error', $error);
    }
}

You can do your Rollback and Commit within the above.

InsiteFX
#8

[eluser]ekstro[/eluser]
InsiteFX - really thanks for your answer, but my problem is quite different. I want to handle MYSQL errors with custom model extension - more precisely: I have custom table with log where when error occurs it insert info about mysql error (and other info, about post req etc.). I don't know why CI didn't collect info that normaly is available in $this->db->_error_number and $this->db->_error_message - and give as access to it exactly like the info about the performance of such queries (in array) (in $this->db we have access to all transaction queries, execution time etc - but why we haven't mysql errors...)
#9

[eluser]InsiteFX[/eluser]
You can post this as a feature request on the CodeIgniter User Voice.

InsiteFX
#10

[eluser]Piter[/eluser]
What is your table in the database engine? Do you have InnoDB? Probably because I only work there.




Theme © iAndrew 2016 - Forum software by © MyBB