创建一个大型数据库,大约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";
我知道这是非常丑陋的代码。我更关心的是在这一点上构建数据库,这是我要展示的成品,以便向前推进并编写干净的代码。
答案 0 :(得分:1)
通过消除过程并查看所涉及的组件,并牢记这一切(显然)非常具有试探性:
max_execution_time
指令等) PHP日志中出现错误。