Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2007
    Posts
    11

    Question Unanswered: Database Transactions

    I have 2 queres........
    Both of them must execute one after another...
    But 2nd query goes through a 'for' loop............
    If once 2nd query fails, all the changes happened to database must be roll back...
    How can i write it....?
    Is this correct..?

    PHP Code:
    $query_inst_slabCode="insert into smt.slabcode (slbc_comcode,slbc_slbcode,slbc_slbname,slbc_qtyval,slbc_unit,slbc_amtper) values ('$txtComCode','$txtSlbCode','$txtSlbName','$slctValQty','$txtUnit','$slctPerCurr')";
    pg_query($dbconn,"begin");
                        if(!
    pg_query($dbconn,$query_inst_slabCode)){
        
    pg_query($dbconn,"rollback");
        
    trigger_error("An error occured..",E_USER_ERROR);
    }
    $arrGrid1Data explode("^",$txtHidGrid1Data);
    $count sizeof($arrGrid1Data);

    for(
    $i=0;$i<($count-1);$i++){                            $comps=explode("|",$arrGrid1Data[$i]);
        
    $query_inst_slabRates="insert into smt.slabrates (slrt_comcode,slrt_slbcode,slrt_lower,slrt_upper,slrt_range,slrt_rate,slrt_effdate,slrt_qtyval) values ('$txtComCode','$txtSlbCode','$comps[0]','$txtUpVal','$comps[4]','$comps[2]','$comps[3]','$slctValQty')";
        if(!
    pg_query($dbconn,$query_inst_slabRates)){
            
    pg_query($dbconn,"rollback");
            
    trigger_error("An error occured..",E_USER_ERROR);
            }
    }
        if(!
    pg_query($dbconn,"commit;")){
            
    pg_query($dbconn,"rollback");
            
    trigger_error("An error occured..",E_USER_ERROR);
        }else{
    $result.="ok";


  2. #2
    Join Date
    Jun 2004
    Location
    Arizona, USA
    Posts
    1,848
    I'm not a PHP guy, but the pseudo code is
    Code:
    SQL: Begin Transaction
    SQL: Insert into ...
    If Error then 
      SQL: RollBack
    Else
      Loop FOR i = 0 to $Count-1
        SQL: insert into ...
        If Error then
          SQL: RollBack
          Exit out
        End If
      End Loop
      SQL: Commit
    EndIf
    Lou
    使大吃一惊
    "Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
    "I have my standards. They may be low, but I have them!" - Bette Middler
    "It's a book about a Spanish guy named Manual. You should read it." - Dilbert


  3. #3
    Join Date
    Jan 2007
    Posts
    11

    Question

    Quote Originally Posted by loquin
    I'm not a PHP guy, but the pseudo code is
    Code:
    SQL: Begin Transaction
    SQL: Insert into ...
    If Error then 
      SQL: RollBack
    Else
      Loop FOR i = 0 to $Count-1
        SQL: insert into ...
        If Error then
          SQL: RollBack
          Exit out
        End If
      End Loop
      SQL: Commit
    EndIf
    Thnx........
    Can anyone do it in PHP n PGSQL..?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •