Kill MySQL query on user abort

后端 未结 6 1435
星月不相逢
星月不相逢 2020-12-05 03:30

When running a long query from PHP, [how] can I kill the query if the user presses stop in their browser?

Take into consideration that I cannot call

6条回答
  •  伪装坚强ぢ
    2020-12-05 03:59

    This is an example of doing it with PDO.

    $id = $connection->executeQuery("SELECT CONNECTION_ID()")->fetchColumn();
    ignore_user_abort(true);
    while($row = $result->fetch()) {
      /* Processing ... */
      if (connection_aborted()) {
        $this->connection->executeQuery("KILL CONNECTION $id");
        break;
      }
    }
    

    Note: Keep an eye out for multiple connections, you may kill the wrong one.

提交回复
热议问题