停止通过PDO查询 [英] Stop query through pdo

查看:67
本文介绍了停止通过PDO查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PDO,用户应该可以选择停止先前触发的请求.

I am using PDO, and the user should have the option of stop the request that he previous triggered.

例如,现在我单击生成报告,但是在请求之后,我忘了再选择一个使报告无用的字段.因此,我需要取消该请求,然后重新提出一个请求.

For example, Now I click generate a report, however after the request I forgot to select one more field that makes the report useless. So I need to cancel the request, and make a new one.

基本上,如何取消正在运行的MYSQL查询?

Basically, how can I cancel the MYSQL query that is running?

if (!isset($_POST['cancel_request'])) { 
    //query
}else{
    //the user cancel the query
}

我知道我可以使用kill命令和PID进程,但这应该通过PDO运行,我不知道什么是PID.

I know that I can use the kill command and the PID process, but this should running through PDO, and I don't know what is the PID.

推荐答案

此处的主要问题是在生成报告的异步请求和应停止报告的脚本之间共享PID.

The main problem here is to share PID between your async request that generates the report and the script that should stop it.

您可以使用以下方法获取PID:

You can get your PID using:

    $stmt = $dbh->prepare("SELECT CONNECTION_ID()");
    $stmt->execute();
    $pid = $stmt->fetchColumn();

您可以使用 php-shared-memory 之类的东西来创建共享变量在您的脚本之间.如果您没有将Composer用于项目依赖性,则此库具有独立发行版(1.5.0,

And you can use something like php-shared-memory to create a shared variable between your scripts. If you're not using Composer for your project dependancies, this library has a standalone release (1.5.0, here).

实施示例:

<?php

if (!include __DIR__ . '/vendor/autoload.php')
{
    die('You must set up the project dependencies.');
}

use Fuz\Component\SharedMemory\SharedMemory;
use Fuz\Component\SharedMemory\Storage\StorageFile;

// your intializations here

$storage = new StorageFile("/tmp/shared.{$user_id}.sync");
$shared = new SharedMemory($storage);

if (!isset($_POST['cancel_request']))
{
    $stmt = $dbh->prepare("SELECT CONNECTION_ID()");
    $stmt->execute();
    $pid = $stmt->fetchColumn();

    $shared->pid = $pid;

    // your long query here

    $shared->destroyStorage();
}
else
{
    // kills pid
    $pid = $shared->pid;
    if (!is_null($pid))
    {
        $dbh->exec("KILL {$pid}");
    }
}

这篇关于停止通过PDO查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆