title: How do I use Interbase transactions with PHP?
body: <p>I have a PHP site connected to an Interbase DB. The DB contains orders which users can load and are displayed on screen. The user can make changes to the order and save them. This works but if 2 users load and save the same record then the order contains the changes made by the last user who saved. </p>
  
  <p>When the 2nd user tries to save I want a message to pop up saying the order has been changed and stop the order from being saved.</p>
  
  <p>I know that interbase has transactions to do this as I have a desktop app that implements transactions and the above scenario. However, I do not know how to do the same thing with PHP in a web environment. </p>
  
  <p>The desktop app keeps the db open all the time and the transaction is kept alive from the time it was read to committed. With PHP the db and transaction is opened/created only when each query is run. From what I read the transaction is rolled back at the end of the script if it's not committed.</p>
  
  <p>Code loading an order</p>
  
  <p>PHP Code:</p>
  
  <pre><code>public function GetOrderDetails($in_OrderID) 
  { 
  
     $qry = "SELECT ID, ... , FROM CUSTOMER_INVOICE WHERE ID = $in_OrderID";    
  
     $this-&gt;dbconn = ibase_connect ($this-&gt;host, $this-&gt;username, $this-&gt;password); 
     $this-&gt;dbtrans = ibase_trans( IBASE_DEFAULT,$this-&gt;dbconn );    
     $result = ibase_query ($this-&gt;dbtrans, $qry);  
  
     while( $row = ibase_fetch_row($qryResult) )  
     {   
     } 
     ibase_free_result($in_FreeQry);      
     ibase_close($this-&gt;dbconn);   
  }  
  </code></pre>
  
  <p>Code saving order</p>
  
  <p>PHP Code:</p>
  
  <pre><code>public function SaveOrderDetails() 
  { 
     $DoCommit = false;   
     try  
     {     
        $this-&gt;dbconn = ibase_connect ($this-&gt;host, $this-&gt;username, $this-&gt;password); 
        $this-&gt;dbtrans = ibase_trans( IBASE_DEFAULT,$this-&gt;dbconn );    
  
        // Insert/Update the order 
        if( $this-&gt;UpdateOrder() ) 
        { 
           // Insert Order Items 
           if( $this-&gt;InsertOrderItems() ) 
           {             
              $DoCommit = true;   
           } 
           else 
           { 
              $this-&gt;ErrorMsg = "ERROR 0003: Order Items could not be inserted";                         
           }       
        } 
        else 
        { 
            $this-&gt;ErrorMsg = "ERROR 0002: Order could not be inserted/updated";              
        } 
  
  
        if( $DoCommit )        
        { 
           if( ibase_commit($this-&gt;dbtrans) ) 
           { 
              $OrderResult = true; 
           } 
           else 
           {  
              ibase_rollback($this-&gt;dbtrans);         
              $this-&gt;ErrorMsg = "ERROR 0004: DB Qry Commit Error";    
              print $this-&gt;ErrorMsg ; 
           }           
        }   
        else 
        { 
           ibase_rollback($this-&gt;dbtrans);   
        }  
     } 
     catch( Exception $e )  
     { 
        ibase_rollback($this-&gt;dbtrans);   
        $this-&gt;ErrorMsg = "ERROR 0001: DB Exception: " . $e;     
     }        
     ibase_close($this-&gt;dbconn);     
  }  
  </code></pre>
  
  <p>If anyone can tell me where I'm going wrong that would be great. Or, if no one uses Interbase how would you do it with MySQL? I don't want to go down the table locking, timestamp route.</p>
  
  <p>Thanks
  Ray</p>