【问题标题】:How to stop SQL query execution from client-side using PHP / JavaScript?如何使用 PHP/JavaScript 从客户端停止 SQL 查询执行?
【发布时间】:2015-08-24 07:18:53
【问题描述】:

我找到了this answer,但它已经很老了,我不确定反复 ping 客户端是否是最好的方法。

我有一个带有按钮START 的HTML 页面,它对另一个PHP 脚本执行AJAX 调用,该脚本本身执行繁重的SQL 查询。一旦完成,结果会以 JSON 格式返回,Javascript 会显示它。

我需要用户能够停止这个请求,所以我添加了一个STOP 按钮。如何使此按钮在单击时停止 SQL 查询(或更一般地说是任何正在运行的 PHP 脚本)?

到目前为止我的想法:

  • 在创建按钮时创建一个独特的id
  • 在发出START 请求时将此id 添加到参数中
  • 创建一个 PHP 页面 abort_request.php 并使用唯一的 id 调用 STOP 按钮
  • 使abort_request.php 将唯一的id 存储到来自$_SESSIONaborted 数组中
  • 定期从 PHP/SQL 脚本检查其 id 是否包含在数组中

这是解决这个问题的好方法还是有一些我没有想到的缺点?

【问题讨论】:

  • 您尝试过交易吗?也许你在点击开始按钮时启动一个,如果你点击停止按钮就停止它......我不确定,这只是一个想法

标签: javascript php sql ajax request


【解决方案1】:

我不知道也从未有过heavy SQL query 的经验,但根据我的经验和我的研究,您可以通过abort();xmlhttpajax 来停止执行,这需要请求执行 sql 的 php 文件。

我的测试:

index.php

var xmlhttp;
function loadXMLDoc(){
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "mysql.php", true);
xmlhttp.send();
}
function stopLoad() {
xmlhttp.abort();
}

mysql.php

mysql_connect("localhost", "root", "");
mysql_select_db("test");
for ($i = 0; $i < 9999; $i ++) {
INSERT INTO `test`.`big-data` (`id`, `a`, `b`, `c`, `d`, `e`, `f`, `g`, `h`, `i`, `j`, `k`, `l`, `m`, `n`, `o`, `p`, `q`, `r`, `s`, `t`, `u`, `v`, `w`, `x`, `y`, `z`) VALUES ('', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test');
}

插入数据需要9.402 秒。 如果我停止大约 3 秒,它将停止执行,但仍保留在我停止之前插入的数百个数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    相关资源
    最近更新 更多