MYSQL关闭连接不能停止插入活动 [英] MYSQL close connect can not halt the insert activity

查看:188
本文介绍了MYSQL关闭连接不能停止插入活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php
header('Content-Type: text/html; charset=utf8');
$dbhost = '192.168.1.23';
$dbuser = 'demouser01';
$dbpass = 'demo*PW';
$dbname = 'testdb1';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error with MySQL connection');
mysql_select_db($dbname);
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER_SET_CLIENT='utf8'");
mysql_query("SET CHARACTER_SET_RESULTS='utf8'");
if(isset($_POST['start'])) {
  $chi = 'chiiiii';
  $id = 1;
  for ($i = 1; $i <= 99999999999; $i++) {
    $eng = substr(str_shuffle(str_repeat("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 5)), 0, 1024);
    $math = substr(str_shuffle(str_repeat("1234567890", 6)), 0, 16);
    $sql = "INSERT INTO test_tb (id,chi,eng,num) VALUES (".$id.",'".$chi."','".$eng."',".$math.")";
    $result = mysql_query($sql) or die('MySQL query error');
    $id++;
  }
  $id = 0;
  sleep(60);
  mysql_close($conn);
} else if(isset($_POST['stop'])) {
  mysql_close($conn);
}
?>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
  <input type="submit" name="start" value="Start Insert">
  <input type="submit" name="stop" value="Stop Insert">
</form>

网站中有两个按钮,插入以重复插入记录,停止插入。但是,我尝试使用MySQL关闭连接,它将停止在PHP中的功能,但活动仍然存在。插入物仍然保持执行。如何解决这个问题?感谢。

There are two buttons in the website, insert to insert a record repeatedly, and stop to halt the insert. However, I tried using the MySQL close connection, it will stop functioning in PHP, but the activity still remains there. The insert still keeps performing. How to fix the problem? Thanks.

推荐答案

由于PHP不允许您在请求之间共享资源(AFAIK),有几种方法可以实现在另一个请求中停止进程的能力:

As PHP does not allow you to share resources between requestes (AFAIK) there are a few ways you can achieve this ability to stop a process in another request:

首先添加此行:

if ( isset ($_POST['start']) ) {
    $_SESSION['continue'] = true;

然后用while替换for语句(看起来你很关心迭代的次数您可以追踪此行为):

and then replace your for statement with while (It seems you don care about number of itterations, if so you can chage this behaviour):

for ($i = 1; $i <= 99999999999; $i++) {

替换为:

while ( $_SESSION['continue'] )

停止插入操作:

mysql_close ($con)

可用于:

$_SESSION['continue'] = false;

注意:不要忘记在脚本开始时调用session_start
所有。

NOTE: Do not forget to call session_start () at beginning of your script. Thats all.

我假设你想要从同一个会话停止页面,如果不是这样,你可以使用ACP矿石SHM。例如:

I assumed that you want to stop page from the same session if this is not the case you can use ACP ore SHM instead. for example:

replace:

$_SESSION['continue'] = true

其中:

apc_store( 'continue', true );

while ( $_SESSION['continue'] )

with:

while ( acp_fetch ('continue') )

$_SESSION['continue'] = false;

其中:

apc_store( 'continue', false );

完成!

这篇关于MYSQL关闭连接不能停止插入活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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