CodeIgniter Forums
Need help with CI Query Error Handling - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Need help with CI Query Error Handling (/showthread.php?tid=37537)



Need help with CI Query Error Handling - El Forum - 01-12-2011

[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";
    }



Need help with CI Query Error Handling - El Forum - 01-13-2011

[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è