在运行长mysql语句时输出文本

时间:2013-07-30 14:32:36

标签: php mysql output

我正在尝试让输出缓冲工作,但页面只是与原始内容一起使用,直到它已经完成执行...

file1.php<

<html>
<body>

      <form action="importscript.php" method="post" enctype="multipart/form-data">
        <p>File: <input type="file" name="import" /></p>
        <p><input type="submit" value="Import" /></p>
      </form>
</body>

发布到...

importscript.php&LT;

<?php
   ob_start();
   set_time_limit(0);

   $lines = get contents of file that was submitted - about 50,000 lines on average

   echo "Importing - " . count($lines) . " rows<br />"; //I want to show this data
   ob_flush();

$sql = <<<SQL
INSERT INTO data (date,time,duration)
VALUES(:date,:time,:duration);
SQL;

   $sth = $db->prepare($sql);

   $sth->bindParam(':date', $date, PDO::PARAM_STR);
   $sth->bindParam(':time', $time, PDO::PARAM_STR);
   $sth->bindParam(':duration', $duration, PDO::PARAM_STR);

   $execution_time = microtime();


   foreach ($lines as $line) {

      $lin                = str_replace('"','',$line);
      $parts              = explode(',',$lin);
      $parts[0]           = str_replace('/', '-', $parts[0]);
      $counter++;

      $date              =  $parts[0];
      $time              =  $parts[1];
      $duration          =  $parts[2];

      $ex = $sth->execute();
   }

   $execution_time = microtime() - $execution_time;
   $execution_time = sprintf('It took %.5f sec', $execution_time);

   echo 'Query Finished.  ' . $execution_time' . '<br /><a href="file1.php/">RESET</a>';
   ob_flush(); //output this
?>

我已经修改了很多脚本,但你可以看到我想要显示一些数据。该脚本平均需要大约5分钟,但浏览器只是继续旋转,就像脚本仍在运行一样。

任何帮助,非常感谢。

1 个答案:

答案 0 :(得分:0)

我建议使用“拆分查询解决方案”。 这意味着您从一个只影响查询的前1000行的查询开始。 完成该脚本后,它会自动重定向到执行第1000行到第2000行的脚本,依此类推,直到完成所有行。 通过这种方式,您可以为用户提供有关操作的持续反馈,并且“旋转车轮”问题已经消失。

它不漂亮,但它很简单,它的工作原理。在需要输出给用户的脚本很长的情况下,它解决了我的问题。

相关问题