Welcome Guest, Not a member yet? Register   Sign In
Need help with CI Query Error Handling
#1

[eluser]Unknown[/eluser]
Hi All,

I am trying to execute an MySql query using the CI Active methods. If the query is malformed, then CI invokes 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 pls.

The code snippets is as below:

Code:
function dbInsertInformationToDB($data_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";
    }
#2

[eluser]Cristian Gilè[/eluser]
Hi dazzlers,

close the transaction at the end of the foreach loop:

Code:
function dbInsertInformationToDB($data_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();
        }
        
        $this->db->trans_complete(); // transaction completed
        
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
        }
        else
        {
            $this->db->trans_commit();
        }
        
        return "ERROR";
    }


Cristian Gilè




Theme © iAndrew 2016 - Forum software by © MyBB