PHP脚本没有超时,没有完成,日志中没有错误

时间:2014-07-15 03:33:17

标签: php mysql

创建一个大型数据库,大约8000行。

结构是:

index.html
    <myform></myform>

$('myform').submit(    $.post('upload.php', data, callback(){ alert("success") }');

鉴于它是如此庞大的插入操作,我设置

set_time_limit(300);

脚本运行大约120秒,插入大约5000行然后停止。

此时它会下载upload.php,没有任何内容。不抛出错误,PHP错误日志为空。

不知道从哪里开始尝试解决此问题?


样品

7/1/2014   20:05:25.6 [GV1     ] ATR_21A_COM_FLT_F              CFN             FAULT      ATR COMM FAULT   

的Javascript

$('#upload-form').submit(function (evt) {

    evt.preventDefault();
    var payload = {
        'alarms' = $('#upload-text').val(),
    };

    $.post("upload.php", payload, function (data) {
        if (data == "success") {
            alert("Success");
        } else {
            alert("Failure");
        }
    });
});

PHP

<?php

set_time_limit(300);

$alarms = $_REQUEST['alarms'];

/********** Explode alarms by line break **********/
$alarms = explode("\r\n", $alarms);

/********** Array to hold values for database **********/
$temp = [
    "datetime"    => "",
    "location"    => "",
    "label"       => "",
    "remainder"   => "",
];

/********** Connect to MySQL **********/
$link = new mysqli('localhost', 'USER', 'PASS', 'DBASE');
if ($link->connect_errno > 0) {
    die("<p>Error connecting to MySQL</p>");
}


foreach ($alarms as $key => $row) {

    $row = trim($row);

    /********** Unset frivelous alarms and continue loop **********/
    $check_regex = ('/(CFN|FAULT|OK)/');
    if (preg_match($check_regex, $row) == 0) {
        unset($alarms[$key]);
        continue;
    }

    /********** Explode space delineation **********/
    $temp_row = explode(" ", $row);

    /********** Unset empty elements and reindex **********/
    $temp_row = array_filter($temp_row);
    $temp_row = array_values($temp_row);

    /********** Convert date/time to datetime **********/
    $temp_date = date("Y-m-d", strtotime($temp_row[0]));
    $temp_time = date("H:i:s", strtotime($temp_row[1])); 
    $temp['datetime'] = trim($temp_date) . " " . trim($temp_time);

    /********** Unset empty elements and reindex **********/
    unset($temp_row[0], $temp_row[1]);
    $temp_row = array_filter($temp_row);
    $temp_row = array_values($temp_row);

    /********** Preg remove [*], not needed **********/
    $temp_row = implode(" ", $temp_row);
    $regex = "/\[.*?\]/";
    $temp_row = preg_replace($regex, " ", $temp_row);

    /********** Explode space delineation **********/
    $temp_row = explode(" ", $temp_row);

    /********** Reindex **********/
    $temp_row = array_filter($temp_row);
    $temp_row = array_values($temp_row);

    /********** Get alarm location **********/
    $temp['location'] = $temp_row[0];

    /********** Unset empty elements and reindex **********/
    unset($temp_row[0]);
    $temp_row = array_filter($temp_row);
    $temp_row = array_values($temp_row);    

    /********** Unset rows that are not faults **********/
    $eject_regex = "/(CFN|FAULT)/";
    if (preg_match($eject_regex, $temp_row[0]) != 1) {
        unset($alarms[$key]);
        continue;
    }

    /********** Unset empty elements and reindex **********/
    unset($temp_row[0], $temp_row[1]);
    $temp_row = array_filter($temp_row);
    $temp_row = array_values($temp_row);    

    /********** Get Alarm Label **********/
    $temp['label'] = implode(" ", $temp_row);

    $insert = $link->prepare("INSERT INTO alarms (`alarm_timestamp`, `alarm_location`, `alarm_label`) VALUES (?, ?, ?)");
    $insert->bind_param('sss', $temp['datetime'], $temp['location'], $temp['label']);

    if ($insert->execute()) {
        $insert->free_result();
    } else {
        die($link->error);
    }
} 

echo "success";

我知道这是非常丑陋的代码。我更关心的是在这一点上构建数据库,这是我要展示的成品,以便向前推进并编写干净的代码。

1 个答案:

答案 0 :(得分:1)

通过消除过程并查看所涉及的组件,并牢记这一切(显然)非常具有试探性:

  • PHP:由于您在localhost上运行,并且没有,因此很可能与PHP相关的问题(max_execution_time指令等) PHP日志中出现错误。
  • MySQL:由于记录正确插入到某一点,因此很可能与MySQL相关的问题。
  • 浏览器:很可能与客户端相关的问题,因为如果浏览器决定取消操作,您可能会看到浏览器错误而不是结果页。
  • Web服务器:因此,它离开了Web服务器。虽然Apache和IIS都使用300秒的默认超时设置,但我可能会在配置中覆盖它。如果你碰巧在Windows上使用Zend Server it seems that the default request timeout there is 120 seconds,它很适合你的问题描述。
相关问题