如何在PHP中使用Interbase事务?

时间:2011-04-07 09:45:53

标签: php interbase

我有一个连接到Interbase DB的PHP站点。数据库包含用户可以加载并显示在屏幕上的订单。用户可以更改订单并保存。这有效但如果2个用户加载并保存相同的记录,则该订单包含上次保存的用户所做的更改。

当第二个用户尝试保存时,我想要弹出一条消息,说明订单已被更改并停止保存订单。

我知道interbase有事务要执行此操作,因为我有一个实现事务的桌面应用程序和上面的场景。但是,我不知道如何在Web环境中使用PHP做同样的事情。

桌面应用程序始终保持数据库打开,并且事务从读取到提交时保持活动状态。使用PHP,只有在运行每个查询时才会打开/创建数据库和事务。根据我的描述,如果事务未提交,则事务将在脚本末尾回滚。

加载订单的代码

PHP代码:

public function GetOrderDetails($in_OrderID) 
{ 

   $qry = "SELECT ID, ... , FROM CUSTOMER_INVOICE WHERE ID = $in_OrderID";    

   $this->dbconn = ibase_connect ($this->host, $this->username, $this->password); 
   $this->dbtrans = ibase_trans( IBASE_DEFAULT,$this->dbconn );    
   $result = ibase_query ($this->dbtrans, $qry);  

   while( $row = ibase_fetch_row($qryResult) )  
   {   
   } 
   ibase_free_result($in_FreeQry);      
   ibase_close($this->dbconn);   
}  

代码保存订单

PHP代码:

public function SaveOrderDetails() 
{ 
   $DoCommit = false;   
   try  
   {     
      $this->dbconn = ibase_connect ($this->host, $this->username, $this->password); 
      $this->dbtrans = ibase_trans( IBASE_DEFAULT,$this->dbconn );    

      // Insert/Update the order 
      if( $this->UpdateOrder() ) 
      { 
         // Insert Order Items 
         if( $this->InsertOrderItems() ) 
         {             
            $DoCommit = true;   
         } 
         else 
         { 
            $this->ErrorMsg = "ERROR 0003: Order Items could not be inserted";                         
         }       
      } 
      else 
      { 
          $this->ErrorMsg = "ERROR 0002: Order could not be inserted/updated";              
      } 


      if( $DoCommit )        
      { 
         if( ibase_commit($this->dbtrans) ) 
         { 
            $OrderResult = true; 
         } 
         else 
         {  
            ibase_rollback($this->dbtrans);         
            $this->ErrorMsg = "ERROR 0004: DB Qry Commit Error";    
            print $this->ErrorMsg ; 
         }           
      }   
      else 
      { 
         ibase_rollback($this->dbtrans);   
      }  
   } 
   catch( Exception $e )  
   { 
      ibase_rollback($this->dbtrans);   
      $this->ErrorMsg = "ERROR 0001: DB Exception: " . $e;     
   }        
   ibase_close($this->dbconn);     
}  

如果有人能告诉我哪里出错了会很棒。或者,如果没有人使用Interbase你会如何使用MySQL?我不想沿着表锁定,时间戳路线。

由于 射线

1 个答案:

答案 0 :(得分:0)

您必须使用主键来避免它。你可以使用generator来获得每个订单的唯一ID。