如何通过 PHP 使用 Interbase 事务?

发布于 2024-10-31 02:57:52 字数 2498 浏览 3 评论 0原文

我有一个连接到 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?我不想走表锁定、时间戳路线。

谢谢 射线

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.

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.

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.

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.

Code loading an order

PHP Code:

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);   
}  

Code saving order

PHP Code:

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);     
}  

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.

Thanks
Ray

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥有 2024-11-07 02:57:52

您必须使用主键来避免它。您可以使用生成器来获取每个订单的唯一 ID。

you must use primary key to avoid it. u can use generator to get unique id for each order.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文