请求实体太大 - 无法将20k记录插入Mysql数据库

时间:2018-01-31 13:14:49

标签: php mysql

我已经检查了堆栈溢出以获得上述解决方案,但是解决方案似乎都没有在共享实时服务器上运行。我有大约20,000行的数据,我想插入MySQL db.Anytime我试试记录接近20k,它经常给出“请求实体太大”的错误。或者有没有办法可以打破批量插入,比如说5000条记录,直到它完成整个记录。请,如果是这种情况,我需要你的帮助,因为我不知道如何将它分成批插入。查看我的ini数据:

memory_limit = 5G
max_execution_time = 10000
max_input_time = -1
post_max_size = 5G
max_input_vars = 100000
file_uploads = On
max_file_uploads = 35
upload_max_filesize = 5G
max_allowed_packet=100000

另外,请参阅我的插入代码:

<?php 
       if(isset($_POST['exportBtn']) && isset($_POST['sflt'])){
        $arr = array();
        foreach($_POST['sflt'] as $key => $value) {
         set_time_limit(0);
         $eflt = mysql_prep($_POST['sflt'][$key]);
         $emodel = mysql_prep($_POST['smodel'][$key]);
         $eengine = mysql_prep($_POST['sengine'][$key]);
         $eloc = mysql_prep($_POST['sloc'][$key]);
         $estye = mysql_prep($_POST['sstye'][$key]);
         $ensvr = mysql_prep($_POST['snsvr'][$key]);
         $eehd = mysql_prep($_POST['sehd'][$key]);
         $epname = mysql_prep($_POST['spname'][$key]);
         $epn = mysql_prep($_POST['spn'][$key]);
         $ecu = mysql_prep($_POST['scu'][$key]);
         $eqty = mysql_prep($_POST['sqty'][$key]);
         $ett = mysql_prep($_POST['stt'][$key]);
         $mtyp = mysql_prep($_POST['sstye'][$key]);
         $mtyp2 = $mtyp=='T'?'T':'S'; 
         $cby = $_SESSION['username'];
         $ct = date('Y-m-d H:i:s');

         array_push($arr,"('$eflt','$emodel','$eengine','$eloc','$estye','$ensvr','$eehd','$epname','$epn','$ecu','$eqty','$ett','$cby','$ct','$mtyp2')");
           }

        $inExp = mysqli_query($link,"INSERT  INTO tab_mydbtrans(fltno,model,engine,loc,serviceType,nextSvr,usageHr,partName,partNo,costUnit,qty,total,createdBy,created_at,mtype)VALUES".implode(',', $arr));

请注意,我已经搜索过,没有人能够解决我的问题,因此这个问题,以免将其标记为重复。

insert multiple rows via a php array into mysql

Request Entity Too Large PHP

1 个答案:

答案 0 :(得分:1)

以下代码将在单个查询中插入100条记录。用户代码中的here,因为此查询对SQL注入攻击开放。

import xlrd
from xlwt import Workbook

data = []
head = []
mergedlist = []
path = "E:/Sreeraj/Thailand/"
file = "1. Ban Kong Niam"
text = path + file + ".txt"
excel = path + file + ".xls"


with open(text) as f:
    for cnt, line in enumerate(f):
        if cnt < 12:
            mergedlist.append(line)

with open(text) as f:
    for line in f:
        data.append([word for word in line.split("\t") if word])

mergedlist = head + data

import xlwt
wb = xlwt.Workbook()
sheet = wb.add_sheet("New Sheet")
for row_index in range(len(mergedlist)):
    for col_index in range(len(mergedlist[row_index])):
        sheet.write(row_index, col_index, mergedlist[row_index][col_index])


wb.save(excel)