【问题标题】:How do I use Interbase transactions with PHP?如何在 PHP 中使用 Interbase 事务?
【发布时间】:2011-04-07 09:45:52
【问题描述】:

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

谢谢 雷

【问题讨论】:

    标签: php interbase


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 2014-01-24
      • 1970-01-01
      相关资源
      最近更新 更多