Query error handling in CodeIgniter
I am trying to execute a MySQL query using the CI active record library. If the query is malformed, then CI invokes an internal server error 500 and quits without processing the next steps.
I need to roll back all the other queries processed before that error statement, and the roll back is also not happening.. can you help please?
The code snippets is as below:
function dbInsertInformationToDB($d开发者_开发技巧ata_array)
{
$returnID = "";
$uniqueDataArray = array();
// I prepare a array of values here
$uniqueTableList = filter_unique_tables($data_array[2]);
$this->db->trans_begin();
// inserting is done here
// when there is a query error in $this->db->insert().. it is not rolling back the previous query executed
foreach($uniqueTableList as $table_name)
{
$uniqueDataArray = filterDataArray($data_array,$table_name,2);
$this->db->insert($table_name,$uniqueDataArray);
if ($this->db->_error_message())
{
$error = "I am caught!!";
}
$returnID = $this->db->affected_rows();
}
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
}
else
{
$this->db->trans_commit();
}
return "ERROR";
}
Try a try catch block: http://php.net/manual/en/language.exceptions.php
精彩评论