Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在提交事务之前冻结php?_Php_Jquery_Logging_Transactions_Commit - Fatal编程技术网

在提交事务之前冻结php?

在提交事务之前冻结php?,php,jquery,logging,transactions,commit,Php,Jquery,Logging,Transactions,Commit,我希望php在提交事务之前冻结,以便在提交事务之前向用户显示更新日志,并提供继续或回滚选项 这是将更新提交到数据库的代码:- <?php include 'submitLogger.php'; // Begin logging ini_set( "error_log", $logFile ); ini_set( "log_errors", "On" ); ini_set( "display_errors", "Off" ); error_log( "Log file '" . $logF

我希望php在提交事务之前冻结,以便在提交事务之前向用户显示更新日志,并提供继续或回滚选项

这是将更新提交到数据库的代码:-

<?php
include 'submitLogger.php';

// Begin logging
ini_set( "error_log", $logFile );
ini_set( "log_errors", "On" );
ini_set( "display_errors", "Off" );
error_log( "Log file '" . $logFile . "' created" );

// Open the database
.
.

error_log( "Connect OK" );
.
.

error_log( "Transaction started (autocommit OFF)\n" ); 
.
.

error_log( "Processing " . count( $deletes ) . " item(s) marked for deletion..." ); 
.
.


// commit changes
error_log( "Committing changes..." ); 
if ( mysqli_commit( $link ) === false ) {
  mysqli_rollback( $link );
  error_log( "Commit failed. Transaction rolled back." ); 
  $response['error'] = "Could not commit changes. Transaction rolled back.";
} else {
  error_log( "Commit successful!" ); 
  $response['success'] = "Success!";
}

// close DB connection
.
.

// Return result
.
.
这是加载上面的更新代码,然后显示日志文件的代码,但在提交事务之后:-

<script type="text/javascript">

$(document).ready( function() {


  // send AJAX request to perform the updates and begin logging
  $.post(
    '../lib/updateMenu.php', 
    sendData,
    function( response ) {
      // was it successful?
      if ( typeof response.success === 'undefined' ) {
        // no - show alert
        if ( response.error ) {
          alert( response.error );
        }
        console.error( "Amend not successful" );
        console.error( response );
        return;
      }

      // delete the AmendmenuAmendselected program window to force a reload on next click
      $( "#programWindowAmendmenuAmendselected" ).remove();
    },
    "json"
  ).error(function(jqXHR, textStatus, errorThrown) {
    alert( 'Unexpected error: ' + textStatus + ' ' + errorThrown );
    console.error( 'Unexpected error during amend: ' + textStatus + ' ' + errorThrown );
    console.error(jqXHR);
  }).complete(function() {
    // once a reply is received, stop the logging
    ajaxLogtail.stopTail();      
  });

  // begin querying log file
  var ajaxLogtail = new AjaxLogtail( '../lib/ajaxLogtail.php?logfile=' + logFile, "programWindowAmendmenuSubmit" );  
  ajaxLogtail.startTail();
});

</script>
如何让php代码在提交事务之前等待前端的响应


有人有什么好主意可以帮你吗?

因为你目前尝试的方式不可能做到这一点,我将给你一个解决问题的方法

我会让我的脚本运行一个外部进程,或者更确切地说是一个持久化进程或worker gearman。工作人员将创建事务并返回对我的主脚本的响应,以向用户提供反馈

需要记录worker实例,以便以后继续。当用户确认后,请求的下一次执行将重新连接到worker,告诉它提交或回滚


需要注意的蠕虫是事务的预期寿命,如果用户等待时间过长,与数据库的连接可能会关闭。

不可能。http不是这样工作的。您不能冻结php然后再继续,也不能在一个http请求中执行clientserver之间的多次往返,而用户提示会这样做require@MarcB,我可以运行两次交易吗?第一次作为诱饵向用户显示日志,第二次是真实的,如果用户选择,不显示日志?非常混乱,有什么好办法吗?没有。当php脚本退出时,将执行清理,关闭数据库连接并回滚事务。如果您尝试使用持久数据库连接,则无法保证用户的响应会得到相同的连接,并且最终会得到一个完全不同的连接,其中没有等待te事务。听起来正是我想在@Flosculus中做的。你能给我一个好的链接,看看如何设置这个工人或我应该只是谷歌gearman?我建议的实现并不像听起来那么简单,除非您熟悉这个概念。试试gearman,不要尝试实现它,只要在程序上练习玩RPC方面,你就会知道如何得到你需要的东西。我已经做了一些调查,gearman不工作至少有两个原因:1。这是一个php到php的体系结构和2。我的更新等是随机的,不能在作业中定义。但是感谢info@FlosculusPHP连接到gearman,所以您在命令行中运行PHP,该脚本打开到gearman的连接并等待。然后,其他脚本连接到gearman并请求完成作业。gearman完全是PHP,只是某种消息代理。